SQL BASICS 3 # Learn SQL for Data Analysis.
MSSQL Topics:
Tables:
Create table
Insert values
Select
Alter table
CREATE:
It consists of SQL commands which is used to define the database schema.
Syntax –
Create table table_name(Store_Details).
INSERT:
1. By adding values for all columns.
Syntax:
INSERT INTO table_name VALUES (val1,val2,val3,….);
Insert 10 rows into the Store Details table
Insert into Store_Details values(1,'Walmart',374,246,'Bentonville,Ark','Montgomery',36104),
(2,'The Kroger Co',115,240,'Cincinnati','Juneau',99801), (3,'Costco',93,567,'Issaquah, Wash','Phoenix',85001), (4,'The Home Depot',91,639,'Atlanta','Little Rock',72201), (5,'Walgreens Boots Alliance',82,484,'Deerfield, Ill','Sacramento',95814), (6,'CVS Health Corporation',79,890,'Woonsocket, R.I','Denver',80202), (7,'Target',71,251,'Minneapolis','Hartford',06103), (8,'Lowe Companies',63,308,'Mooresville, N.C','Dover',19901), (9,'Albertsons Companies',59,454,'Boise, Idaho','Tallahassee',32301), (10,'Royal Ahold Delhaize USA',43,254,'Carlisle, Pa','Atlanta',30303);
2. By both column and values
Syntax:
INSERT INTO table-name (column-names) VALUES (values);
Code - insert into store_details(Store, Store_name, Sales, Order_No, Store_Location, City, pincode) values (11,'Jack and Jones',525,148,'Amblipura','Bangalore',560102);
3. Inserting data in specified columns.
Syntax:
Insert into table_name( col1,col2,col3) values(v1,v2,v3);
Code:
Insert into store_details( Store, Store_name, Sales, City) values (12,'H&M',676,'Mumbai');
SELECT:
1. Select an individual column
Syntax -
select column_name from table_name
CODE:
Select Store_name column from Store_details.
2. Select multiple columns from a table
Syntax -
Select column_name1,column_name2,....,column_nameN from table name
CODE -
select Store_Name,Store_Location,City from Store_Details
3. Select all columns from a table
Syntax:
Select * from table name
CODE:
Select * from Store_Details.
ALTER:
1. By adding column
It is used to add, modify or delete columns in an existing table.
Example:
Add a column profit with datatype int in store_details
Syntax -
ALTER TABLE table_name ADD column_name datatype;
CODE:
alter table store_details add profit int;
2. By Dropping column
Remove the column profit in table store_details
Syntax -
ALTER TABLE table_name DROP COLUMN column_name;
CODE:
Alter table store_details drop column profit.
Upcoming table concepts in next post :
Update table, Delete table, Merge table, Truncate table, Drop table