Examples will be provided on this two tables
Student Table
Sid
Sname
did
1
ahmed
10
2
khalid
10
3
eman
20
4
omar
NULL
Department Table
Did
Dname
10
SD
20
HR
30
IS
40
Admin
Type Of Joins
Cross Join
Cartesian Product
SELECT Sname,DnameFROM Student,Department
SELECT Sname,DnameFROM student CROSS JOIN Department
Sname
Dname
ahmed
SD
khalid
SD
eman
SD
omar
SD
ahmed
HR
khalid
HR
eman
HR
omar
HR
ahmed
IS
khalid
IS
eman
IS
omar
IS
ahmed
Admin
khalid
Admin
eman
Admin
omar
Admin
Inner Join
Equi Join Only Matched Values No null Values from any table FK =PK
These two queries are the same
Sname
Dname
ahmed
SD
khalid
SD
eman
HR
Outer Join
Left Outer Join If You want every row from the left table even if there no matched values
Sname
Dname
ahmed
SD
khalid
SD
eman
HR
Omar
NULL
Right Outer Join If You want every row from the right table even if there no matched values
Sname
Dname
ahmed
SD
khalid
SD
eman
HR
NULL
IS
NULL
ADMIN
Full Outer Join If You want every row from the right and left tables even if there no matched values its combination of Left And Right Outer Join
Sname
Dname
ahmed
SD
khalid
SD
eman
HR
omar
NULL
NULL
IS
NULL
ADMIN
Self Join
Unary Relation Primary Key and foreign key is in the same table
eid
ename
superid
1
ahmed
NULL
2
eman
1
3
khalid
1
4
nada
2
5
ali
2
we want a query to know the employee and his supervisor name
Empname
Supername
eman
ahmed
khalid
ahmed
nada
eman
ali
eman
Examples on Database
TRY IT YOURSELFCross Join
Inner Join
Display student name and department for students from alex
Display student name and all department data for the student
Display student name and department for student for students ages between 20 and 25 then order the result by Department name
Outer Join
Display all Student even it no department match
Self Join
Student Name and his Supervisor
Display Supervisors Name
Display all students even if they have no super
Joins With More than Two Tables
Display Student name and his courses and grades
The same output as the last query
Joining four Tables
Join With DML
Joining two table and update grades based on condition in another table
Built In Functions
ISNull
Replace null values with other value
Coalesce
Replace null values with any of multiple replacement values if first name is null display last name and if its null display address and if null display "Student has no name"
Autoincrement ID
Auto increment For Id
reset identity
Like Statement
_ is one character
% is Zero or more character Names Of student there name starts with "a"
Names Of student there name ends with "a"
Some Patterns
a%h starts with a and ends with h
%d_ the second last char is d
___ three chars word
_m__ four chars word the second char is m
___% at least four char word
ahm% word starts with ahm
[ahm]% word starts with a or h or m
[a-h]% word starts with range from a to h
[(am)(gh)]% word starts with "am" or "gh"
%[%] string that ends with %
%[_]% words contains __
Database Integrity
Domain Integrity
Entity Integrity
refrential Integrity
Range of values
Uniqueness
Relationship
DB Constraints
Data Types
Primary Key constraint
(tiny int => 1byte)
unique constraint => multi columns with unique constraints and allow one NULL
SELECT Sname,Dname
FROM Student,Department
WHERE Department.Did =Student.Sid
SELECT Sname,Dname
FROM Student S INNER JOIN Department D
ON D.Did =S.Sid
SELECT Sname,Dname
FROM Student S LEFT OUTER JOIN Department D
ON S.Did = D.Did
SELECT Sname,Dname
FROM Student S RIGHT OUTER JOIN Department D
ON S.Did = D.Did
SELECT Sname,Dname
FROM Student S FULL OUTER JOIN Department D
ON S.Did = D.Did
SELECT X.ename AS Empname ,Y.ename AS Supername
FROM Employee X ,Employee Y
WHERE Y.eid = X.superid
use ITI
SELECT st_name ,dept_name
FROM Student,Department
use ITI
SELECT st_name ,dept_name
FROM Student S ,Department D
WHERE D.Deopt_Id = S.Dept_Id
use ITI
SELECT st_name ,dept_name
FROM Student S ,Department D
WHERE D.Deopt_Id = S.Dept_Id
AND St_Address ='alex'
use ITI
SELECT st_name ,D.*
FROM Student S ,Department D
WHERE D.Deopt_Id = S.Dept_Id
use ITI
SELECT st_name ,dept_name
FROM Student S ,Department D
WHERE D.Deopt_Id = S.Dept_Id
AND St_age BETWEEN 20 AND 25
ORDER BY Dept_Name
use ITI
SELECT st_name ,dept_name
FROM Student S LEFT OUTER JOIN Department D
WHERE D.Deopt_Id = S.Dept_Id
use ITI
SELECT st_name ,dept_name
FROM Student S Right OUTER JOIN Department D
WHERE D.Deopt_Id = S.Dept_Id
use ITI
SELECT st_name ,dept_name
FROM Student S FULL OUTER JOIN Department D
WHERE D.Deopt_Id = S.Dept_Id
SELECT B.St_name AS StudName, A.st_name AS SuperName
FROM Student A, Student B
--(A,PK,Parent,Supervisor) -- (B,FK,Child,Student)
WHERE A.St_Id = B.St_Super
SELECT DISTINCT A.st_name AS SuperName
FROM Student A, Student B
WHERE A.St_Id = B.St_Super
SELECT DISTINCT A.st_name AS SuperName
FROM Student A LEFT OUTER JOIN Student B
WHERE A.St_Id = B.St_Super
SELECT st_fname,crs_name,grade
FROM Student S,Stud_Course SC , Course C
WHERE S.St_Id =SC.St_Id
AND C.Crs_Id = SC.Crs_Id
SELECT st_fname,crs_name,grade
FROM Student S INNER JOIN Stud_Course SC
ON S.St_Id =SC.St_Id
INNER JOIN
Course C
ON C.Crs_Id = SC.Crs_Id
SELECT st_fname,crs_name,grade
FROM Student S INNER JOIN Stud_Course SC
ON S.St_Id =SC.St_Id
INNER JOIN
Course C
ON C.Crs_Id = SC.Crs_Id
INNER JOIN
Department D
ON D.Dept_Id = S.Dept_Id
UPDATE Stud_Course
SET grade +=10
FROM Student s ,Stud_Course SC
WHERE S.St_Id = sc.ST_Id AND St_address ='alex'
SELECT isnull(St_fname,'student has no name')
FROM Student
SELECT coalesce(St_fname,st_lname,st_address,
'student has no name')
FROM Student
CREATE TABLE Test2(
id INT PRIMARY KEY IDENTITY (1,1),
ename VARCHAR(20)
)
INSERT INTO Test2 VALUES ('ahmed')
dbcc check_ident('test',reseed,0)
CREATE TABLE Test3(
id INT IDENTITY ,
ssn INT PRIMARY KEY,
ename VARCHAR(20)
)
INSERT INTO Test3 VALUES (1234,'ahmed')
INSERT INTO Test3 VALUES (4564,'mohamed')
INSERT INTO Test3 VALUES (1234,'salem')
SELECT *
FROM Student
WHERE St_fname ='ahmed'
SELECT *
FROM Student
WHERE St_fname LIKE 'a%'
SELECT *
FROM Student
WHERE St_fname LIKE '%a'
CREATE TABLE Employee(
eid INT PRIMARY KEY IDENTITY,
ename VARCHAR(20),
eadd VARCHAR(20) DEFAULT 'cairo',
hiredate DATE DEFAULT getdate(),
sal INT ,
overtime INT,
--computed and saved
netsal AS(isnull(sal,0)+isnull(overtime,0)) PRESISTED,
bd DATE,
--computed
age AS (YEAR(getdate())-Year(bd)),
gender VARCHAR(1),
hour_rate INT NOT NULL,
dnum INT,
)
CREATE TABLE Employee(
eid INT IDENTITY,
ename VARCHAR(20),
eadd VARCHAR(20) DEFAULT 'cairo',
hiredate DATE DEFAULT getdate(),
sal INT ,
overtime INT,
--computed and saved
netsal AS(isnull(sal,0)+isnull(overtime,0)) PRESISTED,
bd DATE,
--computed
age AS (YEAR(getdate())-Year(bd)),
gender VARCHAR(1),
hour_rate INT NOT NULL,
dnum INT,
--FOR composite primary key
CONSTRAINT C1 PRIMARY KEY(eid,ename),
CONSTRAINT C2 UNIQUE(sal),
CONSTRAINT C3 CHECK(sal>1000)
CONSTRAINT C4 CHECK(overtime BETWEEN 100 AND 500)
CONSTRAINT C5 CHECK (gender IN('M','F'))
CONSTRAINT C6 FOREIGN KEY(dnum) REFERENCES depts(did)
ON DELETE SET NULL ON UPDATE CASCADE
)