SQL
SQL
Database is collection of data in a format that can be easily accessed (Digital) A software
stored in tables
CREATE DATABASE
db_name;
DROP DATABASE
db_name;
Creating our First
Table
USE db_name;
CREATE TABLE table_name
column_name1 datatype constraint,
);
SQL Datatypes
They define the type of values that can be stored in a
column
SQL
Datatypes
Signed & Unsigned
TINYINT UNSIGNED (0 to
255)
TINYINT (-128 to 127)
Types of SQL
Commands
DDL (Data Definition Language) : create, alter, rename, truncate &
drop
DQL (Data Query Language) :
select
DML (Data Manipulation Language) : select, insert, update &
delete
DCL (Data Control Language) : grant & revoke permission to
users
TCL (Transaction Control Language) : start transaction, commit, rollback
etc.
Database related
Queries
CREATE DATABASE db_name; CREATE
SHOW
DATABASES;
Table related Queries
Create
CREATE TABLE table_name
(column_name1 datatype constraint,
column_name2 datatype constraint, );
Table related
Queries
Select & View ALL columns
SELECT * FROM
table_name;
Table related
Queries
Insert
INSERT INTO table_name
(colname1, colname2)
VALUES (col1_v1, col2_v1),
(col1_v2, col2_v2);
Keys
Primary Key It is a column (or set of columns) in a table that uniquely identifies each
Foreign Key A foreign key is a column (or set of columns) in a table that refers to the primary key in
another table.
UNIQU value
all values in column are different
E
PRIMARY makes a column unique & not null but used only for
KEY one
Constraint
s
FOREIGN prevent actions that would destroy links between
KEY tables
Basic Syntax
database
SELECT col1, col2 FROM table_name;
To Select ALL
SELECT * FROM table_name;
Where Clause
To define some
conditions
COUNT(
value.
) Get Maximum
MAX( )
Marks
MIN( )
SUM( )
AVG(
Get )
Average
marks
Group By Clause
Groups rows that have the same values into summary rows. It collects data
from multiple records and groups the result by one or more column.
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
CHANGE Column
(rename)TABLE table_name CHANGE COLUMN old_name new_name
ALTER
new_datatype new_constraint;
CHANGE Column
(rename)
Table related
Queries
Truncate (to delete table's
data)
TRUNCATE TABLE table_name
;
Joins in SQL
Join is used to combine rows from two or more tables, based on a related column between
them.
Types of
Joins
Outer
Joins
Inner Join
Returns records that have matching values in both
tables
Syntax
SELECT column(s) FROM tableA INNER
JOIN tableB ONtableA.col_name =
tableB.col_name;
SELECT * FROM student INNER JOIN
Inner course ONstudent.student_id =
course.student_id;
Join
course
Example student
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 ONtableA.col_name =
tableB.col_name;
SELECT * FROM student as s
Left LEFT JOIN course as c
course
Example student
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 ONtableA.col_name =
tableB.col_name;
SELECT * FROM student as s
Right RIGHT JOIN course as c
course
Example student
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
Exampl
Join cours
e e
student
Resul
t
Think &
Ans
Qs:Write SQL commands to display the right exclusive join :
Syntax
SELECT column(s) FROM
table as a JOIN table as b
ONa.col_name = b.col_name;
Self
Example
Join
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
Quer
query. It involves 2 select statements. y
Syntax Sub
table_name WHERE
col_name operator
( subquery );
SQL Sub Queries
Example Get names of all students who scored more than
class average.
roll numbers.