0% found this document useful (0 votes)
2 views7 pages

SQL

The document provides a comprehensive overview of SQL commands and concepts, including table creation, data insertion, primary and foreign keys, constraints, and various SQL clauses such as SELECT, WHERE, and JOIN. It also covers aggregate functions, grouping, and updating or deleting records. Additionally, it explains the use of default values, check constraints, and cascading effects in foreign keys.

Uploaded by

patelomkumar0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

SQL

The document provides a comprehensive overview of SQL commands and concepts, including table creation, data insertion, primary and foreign keys, constraints, and various SQL clauses such as SELECT, WHERE, and JOIN. It also covers aggregate functions, grouping, and updating or deleting records. Additionally, it explains the use of default values, check constraints, and cascading effects in foreign keys.

Uploaded by

patelomkumar0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1) CREATE TABLE

Create table table_name(id int primary key,name varchar(50));

2) INSERT VALUES

Insert into table_name (id,name) values(101,”om”);

3) Print whole table

Select * from table_name;

4) Primary Key

Column or set of column that uniquely identifies each row. (2 values under a primary key cant be
same)

There can be only one primary key

5) Foreign Key

Column or set of columns that is the primary key of other table

There can be multiple foreign keys

Used to link 2 tables

6) Constraints

NOT NULL: column values cannot be null

UNIQUE: column values cannot be repeated but many columns can be unique

PRIMARY KEY: can only be used for one column

7) Foreign key syntax


Create table temp(cust_id int, foreign key(cust_id) references customer(id));

Create table temp(cust_id int, foreign key(COLUMN’S NAME THAT HAS TO BE MADE THE FOREIGN
KEY FROM THE CURRENT TABLE) references TABLE_NAME THAT HAS TO BE LINKED(PRIMARY KEY
OF THAT TABLE));

8) DEFAULT

Create table table_name (Column_name data_type DEFAULT 2500);

9) CHECK

Create table table_name(age int check(age>18));

10) Distinct

Select distinct city from student;

Values won’t be repeated

11) WHERE CLAUSE (used to apply condition on rows)

Select * from student where marks>50;

12) IN

Select * from student where city in (“mumbai”,”pune”);

13) LIMIT limits the displayed number of rows

Select * from student limit 3;

14) ORDER BY CLAUSE ASC or DESC default(ASC)

Select * from student order by city ASC;

15) Top 3 marks of students

Select * from student

Order by marks desc

Limit 3;

16) Aggregate functions (performs calculations on set of values and returns one value)

MAX,MIN,AVG,COUNT

Select MAX(marks) from student;

17) GROUP BY

Collects data from multiple records and groups them into one or more column

Generally used with aggregate function

COUNT NUMBER OF STUDENTS IN EACH CITY

Select city,count(rollno)

From student
Group by city;

18) FIND AVG MARKS IN EACH CITY IN ASC ORDER

Select city,AVG(marks)

From student

Group by city

Order by city;

19) FIND TOTAL PAYMENT ACCORDING TO EACH PAYMENT METHOD

Select mode,count(customer)

From payment

Group by mode

20) HAVING CLAUSE (used to apply conditions on groups)

Used when we need to apply any condition after grouping

21) COUNT NUMBER OF STUDENTS IN EACH CITY WHERE MAX MARKS CROSS 90

Select count(rollno),city

From student

Group by city

Having max(marks)>90;

GENERAL ORDER

SELECT cloumns

FROM table_name

WHERE condition

GROUP BY columns

HAVING condition

ORDER BY columns;

22) UPDATE

UPDATE student

SET grade=”O”

WHERE grade=”A”;
WILL REPLACE A WITH O

23) DELETE

DELETE from table_name

WHERE condition;

24) CASCADING for foreign keys

Changes in one table should be done in the other

25) ALTER (used to change the schema) ADD,DROP,RENAME TO,CHANGE,MODIFY

ALTER TABLE student

ADD COLUMN age INT;


26) TRUNCATE (DELETES TABLE DATA)
DROP (DELETES TABLE)

27) JOIN (USED TO COMBINE ROWS FROM TWO OR MORE TABLES BASED ON RELATED
COLUMN BETWEEN THEM)

INNER JOIN

RETURNS RECORDS THAT HAVE MATCHINGN VALUES IN BOTH TABLES

SELECT columns

FROM table(A)

INNER JOIN table(B)

ON table(A).col_name=table(B).col_name;
LEFT JOIN

RETURNS ALL RECORDS FROM THE LEFT TABLE AND THE MATCHED RECORDS FROM THE RIGHT
TABLE

SELECT columns

FROM table(A)

LEFT JOIN table(B)

ON table(A).col_name=table(B).col_name;

FULL JOIN
28) 3rd HIGHEST MARKS IN A CLASS

SELECT rollno

From student

Order by marks desc

Limit 1

Offset 3;

You might also like