SQL Slides (Apna College)
SQL Slides (Apna College)
by
Database
Database is collection of data in a format that can be easily accessed (Digital)
A software application used to manage our DB is called DBMS (Database Management System)
Types of Databases
Relational Non-relational (NoSQL)
Database
Table 1 Table 2
Data Data
What is a table?
Student table
Creating our First Database
Our first SQL Query
DDL (Data Definition Language) : create, alter, rename, truncate & drop
SHOW DATABASES;
SHOW TABLES;
Table related Queries
Create
It is a column (or set of columns) in a table that uniquely identifies each row. (a unique id)
There is only 1 PK & it should be NOT null.
Foreign Key
A foreign key is a column (or set of columns) in a table that refers to the primary key in another table.
There can be multiple FKs.
FKs can have duplicate & null values.
Keys
table1 - Student table2 - City
Constraints
SQL constraints are used to specify rules for data in a table.
PRIMARY KEY makes a column unique & not null but used only for one
Constraints
FOREIGN KEY prevent actions that would destroy links between tables
Basic Syntax
To Select ALL
Comparison Operators : = (equal to), != (not equal to), > , >=, <, <=
COUNT( )
MAX( )
Get Maximum Marks
MIN( )
SUM( )
AVG( )
Count number of students in each city where max marks cross 90.
General Order
SELECT column(s)
FROM table_name
WHERE condition
GROUP BY column(s)
HAVING condition
ORDER BY column(s) ASC;
Having Clause
Similar to Where i.e. applies some condition on rows.
Used when we want to apply any condition after grouping.
Count number of students in each city where max marks cross 90.
Table related Queries
Update (to update existing rows)
UPDATE table_name
SET col1 = val1, col2 = val2
WHERE condition;
Table related Queries
Delete (to delete existing rows)
On Update Cascade
When we create a foreign key using UPDATE CASCADE the referencing rows are updated in the child
table when the referenced row is updated in the parent table which has a primary key.
Table related Queries
Alter (to change the schema)
ADD Column
ALTER TABLE table_name
ADD COLUMN column_name datatype constraint;
DROP Column
ALTER TABLE table_name
DROP COLUMN column_name;
RENAME Table
ALTER TABLE table_name
RENAME TO new_table_name;
Table related Queries
Outer Joins
Inner Join
Returns records that have matching values in both tables
Syntax
SELECT column(s)
FROM tableA
INNER JOIN tableB
ON tableA.col_name = tableB.col_name;
SELECT *
Inner Join FROM student
Example INNER JOIN course
ON student.student_id = course.student_id;
student course
Result
Left Join
Returns all records from the left table, and the matched records from
the right table
Syntax
SELECT column(s)
FROM tableA
LEFT JOIN tableB
ON tableA.col_name = tableB.col_name;
SELECT *
Left Join FROM student as s
Example LEFT JOIN course as c
ON s.student_id = c.student_id;
student course
Result
Right Join
Returns all records from the right table, and the matched records
from the left table
Syntax
SELECT column(s)
FROM tableA
RIGHT JOIN tableB
ON tableA.col_name = tableB.col_name;
SELECT *
Right Join FROM student as s
Example RIGHT JOIN course as c
ON s.student_id = c.student_id;
student course
Result
Full Join
Returns all records when there is a match in either left or right table
Syntax in MySQL
LEFT JOIN
UNION
RIGHT JOIN
Full Join
Example
course
student
Result
Think & Ans
Qs: Write SQL commands to display the right exclusive join :
Syntax
SELECT column(s)
FROM table as a
JOIN table as b
ON a.col_name = b.col_name;
Self Join
Example
Employee
Result
Union
It is used to combine the result-set of two or more SELECT statements.
Gives UNIQUE records.
To use it :
every SELECT should have same no. of columns
columns must have similar data types
columns in every SELECT should be in same order
Syntax
SELECT column(s) FROM tableA
UNION
SELECT column(s) FROM tableB
SQL Sub Queries
A Subquery or Inner query or a Nested query is a query within another SQL query.
SELECT column(s)
FROM table_name
WHERE col_name operator
( subquery );
SQL Sub Queries
Example
Get names of all students who scored more than class average.