SQL
SQL
Data Types
•Data type of a column defines what value the
column can store in table
•Defined while creating tables in database
•Data types mainly classified into three
categories
String: char, varchar, etc
Numeric: int, float, bool, etc
Date and time: date, datetime, etc
Primary and Foreign Keys:
Constraints
• Constraints are used to specify rules for
data in a table
• This ensures the accuracy and reliability of
the data in the table
• Constraints can be specified when the table
is created with the CREATE TABLE statement,
or
• after the table is created with the ALTER
TABLE statement
Commonly used constraints in SQL
• Dropping a column
Syntax
ALTER TABLE table_name
DROP COLUMN column_name;
Delete values in table
• Syntax
DELETE FROM table_name
WHERE condition;
SELECT CLAUSE
SELECT *
CREATE TABLE table_name( FROM table_name;
id INT NOT NULL UNIQUE,
name VARCHAR(50) NOT NULL,
age INT NOT NULL CHECK (age >=18),
gender VARCHAR(10) NOT NULL,
phone VARCHAR(10) NOT NULL,
city VARCHAR(10) NOT NULL DEFAULT ‘Lahore’
);
AS operator
SELECT *
SELECT * FROM table_name
FROM table_name WHERE age = 20;
WHERE age < 20;
SELECT *
SELECT * FROM table_name
FROM table_name WHERE age <> 20;
WHERE age > 20;
SELECT *
FROM table_name
WHERE age != 20;
Logical operator - OR
Pattern Description
‘a%’ Start with “a”
‘%a’ Ends with “a”
‘%am%’ Has “am” in any position
‘a%m’ Starts with “a” and Ends with “m”
‘_a%’ “a” in the second position
‘__a%’ “a” in the third position
‘_oy’ “o” in teh second position and “y” in
third position
LIKE operator
SELECT *
FROM Student
ORDER BY Age ASC, Name DESC
IS NULL
SUM (columnName)
AVG (columnName)
Aggregate functions
Works for
INSERT
UPDATE
DELETE
ROLLBACK and COMMIT
PRIMARY KEY
FOREIGN KEY
PRIMARY KEY
PRIMARY KEY and FOREIGN KEY
INNER JOIN: returns matched records of RIGHT JOIN: returns all records from the RIGHT
table and the matched records in both tables
both tables
PRIMARY KEY
FOREIGN KEY
PRIMARY KEY
INNER JOIN
SELECT column1,column2,column3
FROM table1
INNER JOIN table2
ON
table1.columnName=table2.columnNa
me
INNER JOIN
SELECT column1,column2,column3
FROM table1
LEFT JOIN table2
ON
table1.columnName=table2.columnNa
me
LEFT JOIN
SELECT column1,column2,column3
FROM table1
RIGHT JOIN table2
ON
table1.columnName=table2.columnNa
me
RIGHT JOIN – missing entry
SELECT column1,column2,column3
FROM table1
CROSS JOIN table2
ON
table1.columnName=table2.columnNa
me
INNER JOIN for multiple tables
SELECT columns
FROM table1
INNER JOIN table2
ON table1.columnName=table2.columnName
INNER JOIN table3
ON table1.columnName=table3.columnName
INNER JOIN for multiple tables
City
2 Salman Khan 64 Multan Electronics
Courses
cid Cname Cr_id Cr_name 3 Maryam Khan 55 Islamabad Computer
1 Lahore 1 Computer
2 Islamabad 2 Electronics 4 Sania Mirza 67 Lahore Computer
3 Multan 3 Power
5 Babar Azam 96 Lahore Power
INNER JOIN for multiple tables—
missing entry
Student SELECT * FROM Student s
id Name Percent City Course LEFT JOIN City c ON s.City=c.cid
LEFT JOIN Courses cr ON s.Courses=cr.course_id
1 Rana Umar 75 1 1
2 Salman Khan 64 3 2 id Name Pe C Co ci Cname Cr_i C_nam
rc i urs d d
3 Maryam Khan 55 2 1 en
t t e
4 Sania Mirza 67 1 y
5 Babar Azam 96 1 3
1 Rana Umar 75 1 1 1 Lahore 1 Compute
SELECT columns
SELECT columns FROM table1
RIGHT JOIN table2
FROM table1 ON table1.columnName=table2.columnName
LEFT JOIN table2 RIGHT JOIN table3
ON table1.columnName=table2.columnName ON table1.columnName=table3.columnName
LEFT JOIN table3
ON table1.columnName=table3.columnName
INNER JOIN for multiple tables
Used in conjunction with Aggregate HAVING Clause is used to check a condition after
functions to group rows together, by GROUP BY clause
common values
GROUP BY and HAVING Clause
City Table
cid Cityname
1 Lahore
2 Islamabad
3 Multan
GROUP BY and HAVING Clause