Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

SQL Lab

Lesson 5

1
Lesson topics

 SQL LIKE clause


SQL TOP clause
SQL SUBQUERY

2
SQL Like clauses

The SQL LIKE clause is used to compare a value to


similar values using wildcard operators.
There are two wildcards used in conjunction with the
LIKE operator:
The percent sign (%)
The underscore (_)
The percent sign represents zero, one, or multiple
characters.
The underscore represents a single number or
character.
The symbols can be used in combinations.
3
Example:
Here are number of examples showing WHERE part
having different LIKE clause with '%' and '_' operators:
Statement Description
Finds any values that start with
WHERE SALARY LIKE '200%'
200

Finds any values that have 200


WHERE SALARY LIKE '%200%'
in any position

Finds any values that have 00


WHERE SALARY LIKE '_00%' in the second and third
positions

4
Example cont…
Statement Description
Finds any values that end with
WHERE SALARY LIKE '%2'
2

Finds any values that have a 2


WHERE SALARY LIKE '_2%3' in the second position and end
with a 3

Finds any values in a five-digit


WHERE SALARY LIKE '2___3' number that start with 2 and
end with 3

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:

SELECT TOP 3 * FROM EMPLOYEE


To get top 3 records from Employee table based on their salary:

SELECT TOP 3 * FROM EMPLOYEE


ORDER BY Emp_salary DESC;

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

Some guidelines in using subqueries:


You must enclose a subquery in parenthesis.
A subquery must include a SELECT clause and a
FROM clause.
A subquery can include optional WHERE, GROUP BY,
and HAVING clauses.
8
subQueries cont…
The ORDER BY clause can not be specified in a sub query.
A subquery can not be UNION, only a single SELECT
statement is allowed.
Subqueries can be used with the following SQL
statements along with the comparison and some
logical operators:
SELECT
INSERT
UPDATE
DELETE
Also subqueries can be implemented in the SELECT,
FROM, and WHERE clauses of a SELECT
9statement.
Example:
EMPLOYEE

DEPARTMENT

10
subQueries cont…
Adding Subqueries to the SELECT Clause

SELECT DEPT_ID, Dept_Name,


(
SELECT SUM(Emp_Salary)
This is SubQuery
FROM Employee E
WHERE E.dept_ID = 'D105‘
) AS Sub_Total_Salary
FROM DEPARTMENT d
WHERE d.DEPT_ID ='D105';

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:

SELECT E_age, E_sex


FROM (
SELECT Emp_age AS E_age,
Emp_sex AS E_sex
FROM EMPLOYEE
) AS Emp WHERE E_age > 25;
13
Example cont…
Adding Subqueries to the WHERE Clause:
SELECT * FROM EMPLOYEE e
WHERE e.Dept_ID =
(SELECT d.Dept_ID FROM DEPARTMENT d
WHERE d.Dept_ID='D102');

This statement retrieves all employees that works on


HRM Department.

14
Example cont…
Subqueries with EXISTS or NOT EXISTS:

EXISTS simply tests whether the inner query returns any


row.

 If it does, then the outer query proceeds. If not, the outer


query does not execute, and the entire SQL statement
returns nothing.

The EXISTS operator tests the result of running a


subquery, and if any rows are returned it is TRUE, else it
is FALSE.

NOT EXISTS does the opposite of EXISTS.


15
subQueries cont…
Example:
To get the departments detail which are assigned to any of the
employees:
SELECT * FROM DEPARTMENT d
WHERE exists (Select E.dept_ID
from Employee E
where E.dept_id=d.dept_id );
To get the departments detail which are not assigned to any of
the employees:
SELECT * FROM DEPARTMENT d
WHERE not exists (Select E.dept_ID
from Employee E
where E.dept_id=d.dept_id );
16
subQueries cont…
Since the subquery returns more than 0 row, the EXISTS
condition is true, and the condition placed inside the inner
query does not influence how the outer query is run.
Example:
To get the total amount of salary which is given out for all
department employees:

SELECT SUM(Emp_Salary) AS Total_Salary


FROM Employee
WHERE exists (SELECT Dept_Name
FROM DEPARTMENT
WHERE DEPT_NAME like'C%');
17
subQueries cont…
Subqueries with IN or NOT IN,ANY and ALL:
Example:
To retrieve all employees that works on HRM:
SELECT * from Employee E
where E.dept_id IN (select d.dept_id
From Department d
where dept_name='HRM');
To retrieve all employees that are not working on HRM
SELECT * from Employee E
where E.dept_id NOT IN (select d.dept_id
From Department d
where dept_name='HRM');
18
subQueries cont…
To get the details of employee those have a salary of below the
average:
SELECT * FROM EMPLOYEE
WHERE Emp_salary <ANY(SELECT AVG (Emp_Salary) FROM
EMPLOYEE)
To get an employee younger than all employees works on
Computer Science Department:
SELECT * FROM EMPLOYEE
WHERE Emp_Age < ALL(SELECT Emp_Age
FROM EMPLOYEE WHERE DEPT_ID='D105')
To get the youngest employee:
SELECT * FROM EMPLOYEE
WHERE Emp_Age <= ALL(SELECT Emp_Age FROM EMPLOYEE )

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’:

INSERT INTO HRM_GROUP


(HEmp_ID, HEmp_Fname, HEmp_Lname,HEmp_Sex,
HEmp_Age, HEmp_Salary, Dept_ID)
SELECT * FROM Employee E
WHERE E.Dept_ID='D102';

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.

i.e. when a subquery contains a reference to a table that


also appears in the outer query.
For every row processed by the inner query, the outer
query is processed as well.

The inner query depends on the outer query before it can


be processed.

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.

So,SQL looks outside the subquery, and finds


EMPLOYEE in the outer query.

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:

SELECT SUM(Emp_Salary) AS Sub_Total_Salary


FROM Employee E
WHERE E.Dept_ID IN(SELECT d.Dept_ID
FROM DEPARTMENT d
WHERE DEPT_NAME like'C%‘
)
26
Exercise
1. Get the names of all the Departments which have
an employee whose salary is greater than 3000.
2. Get the Name of Department which is Assigned
to any of the Employee.
3. Get the Name of Department which is not
Assigned to any of the Employee.
4. Get the name of all Departments which are
assigned to more than 2 employees.
5. Get the name of all Departments which is
assigned to female employees.
27
END OF LESSON 5

28

You might also like