Structure Query Language (SQL)
Structure Query Language (SQL)
Lecturers
• Must perform these tasks with minimal user effort and command
structure and syntax must be easy to learn.
• It must be portable.
• An ISO standard now exists for SQL, making it both the formal and
de facto standard language for relational databases.
Operator Meaning
= Equal
<> Not Equal
< Less than
> Greater than
<= Less than or Eqaul
>= Greater than or Eqaul
Data
DB
Khobar 2002
Dr. Ejaz Ahmed 28
Input Tables
Riyadh 654
SELECT city, cars_sold
FROM car_sales; Jeddah 921
Jeddah 752
Khobar
Dr. Ejaz Ahmed 29
Input Tables
Renaming Columns
Example1:
Dhahran 2002 456
SELECT *
FROM car_sales
WHERE city = ‘Dhahran’;
Example2:
Range Search
Selecting all the records whose column values is between the
values specified in the WHERE cluause.
Example:
SELECT *
City Year Sold
FROM car_sales
WHERE cars_sold >= 525 Dhahran 2001 525
AND cars_sold <= 752;
Riyadh 2001 700
OR
SELECT *
Riyadh 2002 654
FROM car_sales
WHERE cars_sold Jeddah 2002 752
BETWEEN 525 AND 752;
BETWEEN test includes the endpoints of range. NOT BETWEEN list the one not in the range.
Dr. Ejaz Ahmed 36
Input Tables
IN
Riyadh 2001 700
(‘Dhahran’, ‘Riyadh’);
Example:
City Year Sold
SELECT *
FROM car_sales Jeddah 2001 921
WHERE city
NOT IN Jeddah 2002 752
(‘Dhahran’, ‘Riyadh’);
Khobar 2002
Example: Example:
SELECT * SELECT *
FROM car_sales FROM car_sales
WHERE WHERE
city LIKE ‘J%’ city LIKE ‘%dd%’
NULL Search
Example 1: Select all cities Example 2: Select all cities where
where the number of cars the number of cars sold is kown.
sold is unkown.
SELECT city
FROM car_sales
SELECT city
WHERE cars_sold IS NOT NULL;
FROM car_sales
WHERE cars_sold IS NULL;
City
City Dhahran
Dhahran
Khoba Riyadh
r Riyadh
Jeddah
Jeddah
Dr. Ejaz Ahmed 41
Input Tables
Dhahran City
Dhahran
Dhahran
Riyadh
Riyadh
Riyadh
Jeddah
Jeddah
Khobar
Jeddah
Khobar
Using DISTINCT in the SELECT clause removes duplicate rows from the output table
Dr. Ejaz Ahmed 42
Sorting
• The ORDER BY clause specifies an order
for displaying the result of a query.
– SQL allows the user to order the tuples in the result of a query by
the values of one or more attributes; the default order is
ascending or increasing.
Example: Sorting
Example: SELECT *
FROM car_sales
ORDER BY city asc, car_sales desc;
The following SELECT statement
sorts the car_sales table in
ascending order of city and City Year Cars_Sold
descending order of car_sales Dhahran 2001 525
columns
Dhahran 2002 456
• COUNT, MIN, and MAX apply to numeric and non-numeric fields, but
SUM and AVG may be used on numeric fields only.
Example : COUNT
Rows city
7 4
Dr. Ejaz Ahmed 48
Input Tables
Example : SUM
• Find the total number Find the number of all the
of all the cars sold from cars_sold in Dhahran from
the car_sales table? the car_sales table?
SELECT SELECT
SUM(cars_sold) as cars_sold SUM(cars_sold) as Dah_cars
FROM car_sales FROM car_sales
WHERE city = ‘Dhahran’
Cars_sold Dah_cars
4008 981
• Find the minimum, maximum, and average cars_sold per year and
per city form the car_sales table
Example: Grouping
• Find the total cars sold in each city from the car_sales table.
City Cars
Dhahran 981
Riyadh 1354
Jeddah 1637
City Cars
Dhahran 981
Riyadh 1354
Jeddah 1637
Dr. Ejaz Ahmed 53
Restricting Groups
• Find the cities who sold a total of more than 1000 cars from the
car_sales table.
City Cars
Riyadh 1354
Jeddah 1637
SELECT *
FROM lecturers
WHERE salary > (
SELECT AVG(salary)
FROM lecturers
Outer select );
SELECT lname
FROM lecturers
WHERE dno IN (
SELECT dno
FROM department
WHERE dname = ‘ICS’
);
SELECT *
FROM Lecturers
WHERE salary > (
SELECT max(salary)
FROM lecturers
WHERE dno = (
SELECT DNO
FROM department
WHERE dname = ‘COE’
)
);
Lname dname
SELECT a.lname, b.dname Ahmed ICS
FROM lecturers a, Amin COE
departments b
WHERE a.dno = b.dno;
Hani ICS
Ageel ICS
Outer Join …
• Inner join of departments
and lecturers tables will
result in the following
Lid Lname dno salary dno dname
output.
1 Ahmed 1 4000 1 ICS
2 Amin 2 3700 2 COE
SELECT a.*, b.* 3 Hani 1 4200 1 ICS
FROM lecturers a, 4 Ageel 1 4000 1 ICS
Departments b
5 Yousef 2 3500 2
WHERE a.dno = b.dno
COE
6 Khalid 2 4500 2 COE
• List all the ICS and COE List all the ICS and COE
faculty salaries. faculty salaries. Include
Remove duplicates duplicates
SELECT salary
FROM lecturers
WHERE dno = (
SELECT dno
FROM departments
where dname= ‘ICS’
)
INTERSECTION
SELECT salary Produces result tables from both
FROM lecturers queries and creates single result
WHERE dno = ( table consisting of those rows that
SELECT dno
FROM departments
are common to both result tables.
WHERE dname= ‘COE’
)
• ANY and ALL may be used with subqueries that produce a single
column of numbers.
SELECT *
FROM Lecturers
WHERE salary > ALL (
SELECT salary
FROM lecturers
WHERE dno = (
SELECT DNO
FROM department
WHERE dname = ‘COE’
)
);
• EXISTS is true if and only if there exists at least one row in result table
returned by subquery.
• Since EXISTS and NOT EXISTS check only for existence or non-existence
of rows in subquery result table, subquery can contain any number of
columns.
Dr. Ejaz Ahmed 85
Input Tables
--- Example using the EXISTS
Operator
• Find all ICS lecturers.
SELECT *
FROM lecturers a
WHERE EXISTS
(
SELECT 1
FROM department b
WHERE a.dno = b.dno
AND b.dname = ‘ICS‘
);
• CREATE TABLE +
• ALTER TABLE +
• DROP TABLE +
• With NOT NULL, system rejects any attempt to insert a null in the
column.
• Foreign keys are often (but not always) candidates for NOT NULL.
Dr. Ejaz Ahmed 91
CREATE TABLE – Example 1
CREATE TABLE Employee
(
fname VARCHAR2(15) NOT NULL,
minit CHAR,
lname VARCHAR2(15) NOT NULL,
ssn CHAR(9),
bdate DATE,
address VARCHAR2(50),
sex CHAR,
salary NUMBER(10,2) NOT NULL,
Superssn CHAR(9),
dno NUMBER(3) NOT NULL,
CONSTRAINT employee_ssn_pk PRIMARY KEY(ssn),
CONSTRAINT employee_superssn_fk
FOREIGN KEY(Superssn) REFERENCES employee(ssn),
CONSTRAINT employee_dno_fk
FOREIGN KEY(dno) REFERENCES department(dnumber),
);
• INSERT Statement +
• UPDATE Statement +
• DELETE Statement +
Example:
UPDATE table_name
SET column_name1 = data_value1
[, column_name2 = data_value2...]
[WHERE search_condition]
UPDATE staff
SET salary = salary*1.03;
UPDATE employee
SET salary = salary*1.05
WHERE dno = 1;
UPDATE employee
SET dno = 2
, salary = 4000
WHERE ssn = ‘111’;
• Rows are explicitly deleted from only one table at a time. However,
the deletion may propagate to rows in other tables if referential
triggered actions are specified in the referential integrity constraints
of the DDL.
• The WHERE clause is optional; if omitted, all rows are deleted from
table. But if it is included only those rows that satisfy the
search_condition are deleted.