Professional Documents
Culture Documents
SQL Lesson 5
SQL Lesson 5
Lesson 5
1
Lesson topics
2
SQL Like clauses
4
Example cont…
Statement Description
Finds any values that end with
WHERE SALARY LIKE '%2'
2
5
SQL TOP clauses
The SQL TOP clause is used to fetch a TOP N number records
from a table.
Syntax: SELECT TOP number C_Name FROM table_name
Example:
To get top 3 records from Employee table:
6
SQL subQueries
A subquery—also referred to as an inner query or
inner select—is a SELECT statement embedded
within a data manipulation language (DML)
statement or nested within another subquery.
Subqueries are nested SELECT statement.
a subquery is used when you know how to search
for a value using a SELECT statement, but do not
know the exact value.
Subqueries are an alternate way of returning data
from multiple tables.
subquery is usually added in the WHERE Clause of
the SQL statement.
7
subQueries cont…
A DML statement that includes a subquery is referred
to as the outer query.
A sub query must produce a single column of data as
its result.
i.e. the sub query can have only a single select item in its
SELECT clause
DEPARTMENT
10
subQueries cont…
Adding Subqueries to the SELECT Clause
11
subQueries cont…
The subquery is inserted as the third column
expression in the SELECT list and named the column
Sub_Total_Salary.
The subquery itself is enclosed in parentheses and
made up of a single SELECT statement.
The statement retrieves the total salary of Employee
which is spent for Computer science Department
only.
Adding Subqueries to the FROM Clause
Subqueries are legal in a Select statement's FROM
clause.
The actual syntax is:
12
SELECT ... FROM (subquery) [AS] name ...
subQueries cont…
The [AS] name clause is mandatory, because every table in a
FROM clause must have a name.
Any columns in the subquery select list must have unique
names.
Example:
14
Example cont…
Subqueries with EXISTS or NOT EXISTS:
19
subQueries cont…
Subquery with INSERT statement:
Subquery can be used with INSERT statement to add
rows of data from one or more tables to another table.
Lets try to group all the employees who works on HRM
in a table ‘HRM_GROUP’:
20
subQueries cont…
If you want to insert the whole column values of
EMPLOYEE table to HRM_GROUP table, you can
remove the list of attributes. Like this:
INSERT INTO HRM_GROUP
SELECT * FROM Employee E WHERE E.Dept_ID='D105'
You can also insert some column values to
HRM_GROUP table from EMPLOYEE table by listing
of the column names. Like this:
INSERT INTO
HRM_GROUP(HEmp_ID,HEmp_Fname,HDept_ID)
SELECT Emp_ID, Emp_Fname, Dept_ID
FROM Employee E WHERE E.Dept_ID='D102'
21
subQueries cont…
Subquery with UPDATE statement:
To update the salary of the Employees those works on
computer science department only by 50% :
UPDATE EMPLOYEE
SET Emp_Salary= Emp_Salary *1.5
WHERE DEPT_ID IN (SELECT d.DEPT_ID
FROM DEPARTMENT d
WHERE d.DEPT_ID =‘D105')
It is the same as:
UPDATE EMPLOYEE SET Emp_Salary= Emp_Salary
*1.5
WHERE DEPT_ID=‘D105’
22
subQueries cont…
Subquery with DELETE statement:
To delete employee records those have a salary of below
the average:
DELETE FROM EMPLOYEE
where Emp_salary < (select AVG (Emp_Salary)
from EMPLOYEE)
To delete computer science department Employees
DELETE FROM EMPLOYEE
WHERE DEPT_ID IN (SELECT d.DEPT_ID
FROM DEPARTMENT d
WHERE d.DEPT_ID =‘D105')
23
subQueries cont…
Correlated Subqueries
A query is called correlated subquery when both the inner
query and the outer query are interdependent.
24
subQueries cont…
Example:
SELECT * FROM EMPLOYEE e
WHERE e.DEPT_ID=ANY(SELECT d.DEPT_ID
FROM DEPARTMENT d
WHERE d.DEPT_ID != e.DEPT_ID)
Notice that the subquery contains a reference to a column
of EMPLOYEE table, even though the subquery's FROM
clause does not mention a table EMPLOYEE.
25
subQueries cont…
If a subquery is not dependent on the outer query it is called
a non-correlated subquery.
It can executed on its own without relying on main
outer query.
Example:
28