Week 7.1
Week 7.1
► The SQL GROUP BY clause is used in collaboration with the SELECT statement
to arrange identical data into groups.
► SYNTAX SELECT column1, column2
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2
► The SQL HAVING clause is used in combination with the GROUP BY clause to
restrict the groups of returned rows to only those whose the condition is
TRUE.
► Similar to WHERE, but WHERE filters individual rows whereas HAVING filters
groups.
► DISTINCT has no effect with MIN/MAX, but may have with SUM/AVG.
► Aggregate functions can be used only in SELECT list and in HAVING clause.
45 92 66
6 1 8
Aliasing Table Names
► A table alias is created by directly placing an alias after the table name in the
FROM clause.
► For example in the following example we will refer to departments table as d
or dept.
Main
query: Which employees have salaries
greater than Abel’s salary?
► The subquery (inner query) executes once before the main query (outer
query).
► The result of the subquery is used by the main query.
► Example.
SELECT last_name
FROM employees
WHERE salary >
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
Nested queries
► From the Lecturer table, select lecturers whose salary is above average
► Cannot write 'WHERE salary > avg(salary);
► Solution
► Select * from lecturer where salary>(
select AVG(salary
from lecturer);
► List the names of all Lecturers who are in the ICS department
select name from lecturer
where dno IN(
select dno from department
where dname=‘ICS’
);
Nested queries
► Find lecturers whose salary higher than the salary of at least 1 COE lecturer.
Nested queries
► Find lecturers whose salary higher than the salary of at least 1 COE lecturer.
► Find lecturers whose salary higher than the salary of every COE lecturer
Nested queries
► Find lecturers whose salary higher than the salary of every COE lecturer
Select * from lecturer
Where salary>( select max(salary
from lecturer where dno =(select DNO
from department
where dname=‘COE’);
);
Special Operators
► EXISTS
► Used to check if subquery returns any rows
►
Syntax SELECT COLUMN(S) FROM Table-name
WHERE EXISTS ( subquery );
► EXAMPLE SELCT * FROM EMPLOYEE
WHERE EXISTS( SELECT * FROM DEPARTMENT
WHERE
EMPLOYEE.Dno=DEPARTMENT.Dnumber);
► Find all ICS lecturer
SELCT * FROM LECTURER
WHERE EXISTS( SELECT * FROM DEPARTMENT
WHERE
LECTUREER.Dno=DEPARTMENT.Dnumber AND
DNAME=‘ICS’);
► find all of the records from the customers table where there is at least one
record in the orders table with the same customer_id.
► find all of the records from the customers table where there is at least one
record in the orders table with the same customer_id.
► SOLUTION
SELECT * FROM customers
WHERE EXISTS (
SELECT * FROM orders
WHERE
customers.customer_id = orders.customer_id);
NOT EXISTS Operators
► Query: List first and last name of employees who work on ALL projects NOT controlled
by Dno=5.
SELECT *
FROM lecturers a
WHERE NOT EXISTS
(
SELECT *
FROM department b
WHERE a.dno = b.dno
AND b.dname = ‘ICS‘
);
Not Exists
customers.customer_id = orders.customer_id);
Not Exists
Find the record of all those customer who doesn’t place any
order
Correlated queries
► A correlated subquery is a subquery that uses the values of the outer query. In
other words, the correlated subquery depends on the outer query for its values.
► Because of this dependency, a correlated subquery cannot be executed
independently as a simple subquery.
► Moreover, a correlated subquery is executed repeatedly, once for each row
evaluated by the outer query. The correlated subquery is also known as a
repeating subquery
► The main difference between a SQL correlated subquery and a simple subquery is that
a SQL correlated subquery references columns from the table of the outer query.
Example 1
Select Product_name.list_price,category_id
From production,product p1
Where list_price IN(
select MAX(p2.list_price)
From production,product p2
where p2.category_id=p1.Category_id
Group by p2.category_id)
order by
Name_list, category_id
Correlated queries
► Used to affect row by row processing, each subquery is executed once for
every row of the outer query
“It is a sub query that uses the value of outer query
It is top down approach
GET
Candidate row
Execute
Inner query using candidate row values
Use
Value(s) from inner query to qualify candidate row
EXAMPLE 2
Having count(st-id)>=3)
By correlated queries
Select * from STD where 3>=(select count (st_id) from Registration
where st_id=STD.St_id)
EXAMPLE 2
SELECT po_num, ship_date FROM orders main WHERE 10 > (SELECT COUNT
(DISTINCT ship_date) FROM orders sub WHERE sub.ship_date < main.ship_date) AND
ship_date IS NOT NULL ORDER BY ship_date, po_num;
EXAMPLE 2
► Syntax
INSERT INTO TABLE NAME(FIELD1,FIELD2,…..FIELDN)
VALUES(VALUE1,VALUE2,…….VALUEN);
► EXAMPLE
INSERT INTO
EMPLOYEE(Fname, Minit,Lname,SSN,Bdate,Address,Gender,MSSN,Dno)
VALUES(‘Richard’,’K’,’Marini’,’653298653’,’1962-12-30’,’98 Oak Forest,
Katy, Tx’,’M’,37000,’653298653,4);
► OR
The Delete Command