0% found this document useful (0 votes)
2 views

lab 6

The document provides a comprehensive overview of SQL operations, including creating tables, performing various types of joins (natural, inner, outer), and using the WHERE clause with different operators. It also covers SQL commands for updating, deleting, and selecting data, along with functions like MIN, MAX, COUNT, AVG, and SUM. Additionally, it discusses the use of aliases and logical operators in SQL queries.

Uploaded by

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

lab 6

The document provides a comprehensive overview of SQL operations, including creating tables, performing various types of joins (natural, inner, outer), and using the WHERE clause with different operators. It also covers SQL commands for updating, deleting, and selecting data, along with functions like MIN, MAX, COUNT, AVG, and SUM. Additionally, it discusses the use of aliases and logical operators in SQL queries.

Uploaded by

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

Ajenda:

Perform Joins (Natural, Inner, Outer,


Where Clause
Operators used in SQL
Logical Operators

1. Database Systems SQL JOIN

✓ CREATE TABLE Branch(


branchNo VARCHAR(15) NOT NULL,
street VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
postcode VARCHAR(15) NOT NULL,
CONSTRAINT pk_branches PRIMARY KEY(branchNo)
);
✓ CREATE TABLE Staff(
staffNo VARCHAR(10) NOT NULL
fName VARCHAR(15) NOT NULL,
lName VARCHAR(15) NOT NULL,
position VARCHAR(15) NOT NULL,
sex VARCHAR(15) NOT NULL,
DOB DATE NOT NULL,
salary INT(15) NOT NULL,
branchNo VARCHAR(15) NOT NULL,
CONSTRAINT pk_staffs PRIMARY KEY(staffNo),
CONSTRAINT fk_staffs FOREIGN KEY(branchNO) REFERENCES
Branch(branchNo)
);
✓ CREATE TABLE PrivateOwner(
ownerNo VARCHAR(15) NOT NULL,
fName VARCHAR(15) NOT NULL,
lName VARCHAR(15) NULL,
address VARCHAR(50) NOT NULL,
telNo VARCHAR(15) NOT NULL,
eMail VARCHAR(20) NOT NULL,
CONSTRAINT pk_privateOwners PRIMARY KEY(ownerNo)
);
✓ CREATE TABLE PropertyForRent(
propertyNo VARCHAR(15) NOT NULL,
street VARCHAR(20) NOT NULL,
city VARCHAR(20) NULL,
postcode VARCHAR(15) NOT NULL,
TYPE VARCHAR(15) NOT NULL,
rooms INT NOT NULL,
rent INT NOT NULL,
ownerNo VARCHAR(15) NOT NULL,
staffNo VARCHAR(15) NULL,
branchNo VARCHAR(10) NOT NULL,
CONSTRAINT pk_propertyForRents PRIMARY KEY(propertyNo),
CONSTRAINT fk_propertyForRents FOREIGN KEY(ownerNO)
REFERENCES PrivateOwner(ownerNo),
CONSTRAINT fk_property FOREIGN KEY(staffNo) REFERENCES
Staff(staffNo),
CONSTRAINT fk_for FOREIGN KEY(branchNo) REFERENCES
Branch(branchNo)
);
✓ INSERT INTO Branch VALUES ('B005', '22 Deer Rd',
'London', 'SW1 4EH');
INSERT INTO Branch VALUES ('B007', '16 Argyll St',
'Aberdeen', 'AB2 3SU');
INSERT INTO Branch VALUES ('B003', '163 Main St',
'Glasgow', 'G11 9QX');
INSERT INTO Branch VALUES ('B004', '32 Manse Rd',
'Bristol', 'BS99 1NZ');
INSERT INTO Branch VALUES ('B002', '56 Clover Dr',
'London', 'NW10 6EU');

✓ INSERT INTO Staff VALUES ('SL21', 'John',


'White','Manager', 'M', '1945-10-01', 30000, 'B005');
INSERT INTO Staff VALUES ('SG37', 'Ann',
'Beech','Assistant', 'F', '1960-10-11', 12000, 'B003');
INSERT INTO Staff VALUES ('SG14', 'David','Ford',
'Supervisor','M', '1958-11-24', 18000, 'B003');
INSERT INTO Staff VALUES ('SA9', 'Mary', 'Howe',
'Assistant', 'F', '1970-02-19', 9000, 'B007');
INSERT INTO Staff VALUES ('SG5',
'Susan','Brand','Manager', 'F', '1940-06-03', 24000,
'B003');
INSERT INTO Staff VALUES ('SL41', 'Julie','Lee',
'Assistant', 'F', '1965-06-13', 9000, 'B005');
✓ INSERT INTO PrivateOwner VALUES ('CO46', 'Joe', 'Keogh',
'2 Fergus Dr, Aberdeen AB2 7SX','01224-861212'
,'[email protected]');
INSERT INTO PrivateOwner VALUES ('CO87',
'Carol','Farrel','6 Achray St, Glasgow G32 9DX', '0141-357-
7419','[email protected]');
INSERT INTO PrivateOwner VALUES ('CO40', 'Tina',
'Murphy','63 Well St, Glasgow G42', '0141-943-
1728','[email protected]');
INSERT INTO PrivateOwner VALUES ('CO93', 'Tony', 'Shaw',
'12 Park Pl, Glasgow G4 0QR', '0141-225-
7025','[email protected]');
✓ INSERT INTO PropertyForRent VALUES ('PA14', '16 Holhead',
'Aberdeen', 'AB7 5SU', 'House',6, 650, 'CO46', 'SA9',
'B007');
INSERT INTO PropertyForRent VALUES ('PL94', '6 Argyll St',
'London', 'NW2', 'Flat', 4, 400, 'CO87', 'SL41',
'B005');
INSERT INTO PropertyForRent VALUES ('PG4', '6 Lawrence
St','Glasgow', 'G11 9QX', 'Flat', 3, 350, 'CO40', NULL,
'B003');
INSERT INTO PropertyForRent VALUES ('PG36', '2 Manor Rd',
'Glasgow', 'G32 4QX', 'Flat', 3, 375, 'CO93', 'SG37',
'B003');
INSERT INTO PropertyForRent VALUES ('PG21', '18 Dale Rd',
'Glasgow', 'G12', 'House',5, 600, 'CO87', 'SG37',
'B003');
INSERT INTO PropertyForRent VALUES ('PG16', '5 Novar Dr',
'Glasgow', 'G12 9AX', 'Flat', 4, 450, 'CO93', 'SG14',
'B003');
SQL Joins
✓ Used to combine rows from two or more tables, based on a reference column between
them.

Table Joining using Where Clause


SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2
SELECT Staff.staffno, Staff.fname ,Staff.branchno, Branch.city
FROM Branch, Staff
Where Branch.branchno = Staff.branchno;

Alternative to where for Joining tables


SELECT *
FROM table1 JOIN table2……
[ON (join_condition)]

✓ SELECT Staff.staffno, Staff.fname ,Staff.branchno,


Branch.city
FROM Branch join Staff
ON Branch.branchno = Staff.branchno;
Joining More than Two Tables

✓ select staff.staffno, staff.fname,


propertyforrent.propertyno, privateowner.ownerno,
privateowner.fname
From Staff,PropertyForRent, PrivateOwner
Where staff.staffno = propertyforrent.staffno
AND propertyforrent.ownerno = privateowner.ownerno;
Joining More than Two Tables Alternative Approach
select staff.staffno, staff.fname,
propertyforrent.propertyno, privateowner.ownerno,
privateowner.fname
From Staff Join PropertyForRent
ON staff.staffno = propertyforrent.staffno
Join PrivateOwner
ON propertyforrent.ownerno = privateowner.ownerno;

Can add condition


✓ select staff.staffno, staff.fname, propertyforrent.propertyno,
privateowner.ownerno, privateowner.fname
From Staff, PropertyForRent, PrivateOwner
Where staff.staffno = propertyforrent.staffno
AND propertyforrent.ownerno = privateowner.ownerno
AND Privateowner.fname <> 'Joe'

Use of Alias with Table name


✓ select S.staffno, S.fname, P.propertyno, O.ownerno,
O.fname
From Staff S, ProperForRent P, PrivateOwner O
Where S.staffno = P.staffno
AND P.ownerno = O.ownerno
AND O.fname <> ‘Joe’

SQL INNER JOIN


The INNER JOIN keyword selects records that have matching values in both tables.

✓ SELECT Staff.staffno, Staff.fname ,Staff.branchno,


Branch.city
FROM Branch inner join Staff
ON Branch.branchno = Staff.branchno;
We need to retrieve data of staff name their numbers, their city
where they are working along with their branch number.
SELECT Staff.staffno, Staff.fname ,Staff.branchno, Branch.city
FROM Branch
INNER JOIN Staff
ON Branch.branchno = Staff.branchno;

Inner Join three or more tables


✓ select staff.staffno, staff.fname,
propertyforrent.propertyno, privateowner.ownerno,
privateowner.fname
From ((Staff
inner join propertyforrent on staff.staffno =
propertyforrent.staffno)
inner join privateowner on propertyforrent.ownerno =
privateowner.ownerno);
SQL INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
select * from branch inner join staff on branch.branchno =
staff.branchno

Database Systems
✓ Where Clause
✓ Operators used in SQL
✓ Logical Operators
Where Clause
✓ Used to filter the records
✓ Used with select statement
✓ Used to update data of records
✓ Used to delete records
✓ Can also use to join more than one table

Operators used in SQL


Where clause
✓ SELECT column1, column2, ...
FROM table_name
WHERE condition;
✓ Select * from branch where branchNo =‘B003’
✓ Select * from staff where salary = 30000

Use of different operators within condition


✓ Select * from staff where salary >= 18000

All values except…18000


✓ Select * from staff where salary <> 18000

Use of between in where clause


✓ Select * from staff where salary BETWEEN 12000 AND 18000
✓ Select * from staff where salary NOT BETWEEN 12000 AND
18000

Use of IN Operator
✓ Select * from staff where position IN
('MANAGER','SUPERVISOR')

✓ Select * from staff where position NOT IN


('MANAGER','SUPERVISOR')
LIKE operator and wild card (%, _)
✓ select * from branch where postcode LIKE 'sw1%'

✓ select * from branch where postcode LIKE ‘AB2 3S_’

Logical Operators OR, AND, NOT


Logical Operator OR
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
We need the branch details for London, Bristol and Glasgow city.
✓ Select * from branch where city = 'London' OR
city ='Bristol' OR city = 'Glasgow'
Logical Operator AND
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
✓ Select * From Branch WHERE city = 'London' AND branchNo=
'B002' AND postcode LIKE 'NW10%‘

Logical Operator NOT


✓ SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
✓ Select * From Branch WHERE NOT city = 'London‘
✓ Select * From Branch WHERE NOT city = 'London' AND NOT
city = 'Bristol'

IS NULL , IS NOT NULL


✓ SELECT column_names
FROM table_name
WHERE column_name IS NULL;
✓ Select * From staff WHERE branchNo IS NULL
✓ Select * From staff WHERE branchNo IS NOT NULL
Database Systems
Branch

Staff

Agenda
✓ SQL Update
✓ SQL Delete
✓ SQL Select Top
✓ SQL ORDER BY
✓ SQL Aliases
✓ SQL MIN() and MAX() Functions
✓ SQL COUNT(), AVG() and SUM() Functions
The SQL UPDATE Statement
✓ UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
✓ Update staff
set salary = 20000
where staffno='SG14’

✓ Update staff
set salary = 20000
UPDATE Multiple Records
✓ Update staff
set position='Supervisor’
where position='Assistant’

✓ Update staff
set salary=17000
where position='Supervisor' AND salary < 17000
The SQL DELETE Statement
✓ DELETE FROM table_name WHERE condition;
✓ DELETE FROM Staff WHERE staffno = 'SA9’
✓ DELETE FROM Staff WHERE staffno = ‘SL21’ AND
BranchNo=‘B005’
✓ DELETE FROM Staff WHERE staffno = ‘SA9’

SQL TOP, LIMIT


SQL Server
✓ SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
✓ SELECT TOP 3 * FROM Staff;
✓ SELECT TOP 50 PERCENT * FROM staff;
✓ SELECT TOP 3 * FROM Branch
WHERE city=‘London';
MySQL
✓ SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
✓ SELECT * FROM Staff LIMIT 3;
✓ SELECT * FROM Branch
WHERE City=‘London'
LIMIT 3;

The SQL ORDER BY Keyword


✓ SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
✓ SELECT * FROM Staff
ORDER BY fname;
✓ SELECT * FROM Staff
ORDER BY lname DESC;
✓ SELECT * FROM Staff
ORDER BY Position, Salary;
✓ SELECT * FROM Staff
ORDER BY Position ASC, Salary DESC;

SQL Aliases
✓ SQL aliases are used to give a table, or a column in a table, a temporary name.
✓ Aliases are often used to make column names more readable.
✓ An alias only exists for the duration of the query.

Aliases can be useful when:


✓ There are more than one table involved in a query
✓ Functions are used in the query
✓ Column names are big or not very readable
✓ Two or more columns are combined together.
MySQL/SQL Server
✓ SELECT StaffNo, CONCAT(fname,', ‘,lname) AS FullName
FROM Staff;
The SQL MIN() and MAX() Functions
✓ SELECT MIN(column_name)
FROM table_name
WHERE condition;
✓ SELECT MAX(column_name)
FROM table_name
WHERE condition;
✓ SELECT MIN(salary) AS MinmumSalary
FROM Staff;
✓ SELECT Max(salary) AS MaximumSalary
FROM Staff;

The SQL COUNT(), AVG() and SUM() Functions


✓ SELECT COUNT(staffno) AS [Total Staff] FROM Staff;
✓ SELECT COUNT(branchno) AS [Total Branches] FROM Branch;
✓ SELECT AVG(Salary) As [average Salary] FROM Staff ;
✓ SELECT AVG(Salary) As [average Salary] FROM Staff
where branchno=‘B003’ ;

NULL Values are ignored


✓ SELECT SUM(salary) As [Total Salary] FROM Staff;

Summary
✓ SQL Update
✓ SQL Delete
✓ SQL Select Top
✓ SQL ORDER BY
✓ SQL Aliases
✓ SQL MIN() and MAX() Functions
✓ SQL COUNT(), AVG() and SUM() Functions

You might also like