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

Nested Queries

• In SQL, a nested query involves a query that is


placed within another query. Output of the
inner query is used by the outer query. A
nested query has two SELECT statements: one
for the inner query and another for the outer
query.
• SELECT column1, column2, ... FROM table1
WHERE column1 IN ( SELECT column1 FROM
table2 WHERE condition );
Operators
IN Operator
• This operator checks if a column value in the
outer query's result is present in the inner
query's result. The final result will have rows
that satisfy the IN condition.
NOT IN Operator
• This operator checks if a column value in the
outer query's result is not present in the inner
query's result. The final result will have rows
that satisfy the NOT IN condition.
ALL Operator
• This operator compares a value of the outer
query's result with all the values of the inner
query's result and returns the row if it
matches all the values.
ANY Operator
• This operator compares a value of the outer
query's result with all the inner query's result
values and returns the row if there is a match
with any value.
• EXISTS Operator
This operator checks whether a subquery
returns any row. If it returns at least one row.
EXISTS operator returns true, and the outer
query continues to execute. If the subquery
returns no row, the EXISTS operator returns
false, and the outer query stops execution.
Table: employees table Table: departments table

emp_id emp_name dept_id


1 John 1 dept_id dept_name

2 Mary 2 1 Sales

3 Bob 1 2 Marketing

4 Alice 3 3 Finance
5 Tom 1
Table: sales table
sale_id emp_id sale_amt
1 1 1000
2 2 2000
3 3 3000
4 1 4000
5 5 5000
6 3 6000
7 2 7000
• Find the names of all employees in the sales
department:
SELECT emp_name FROM employees WHERE
dept_id IN (SELECT dept_id FROM departments
WHERE dept_name = 'Sales');

emp_name
John
Bob
Tom
• Find the names of all employees who have
made a sale:
SELECT emp_name FROM employees WHERE
EXISTS (SELECT emp_id FROM sales WHERE
employees.emp_id = sales.emp_id);

emp_name
John
Mary
Bob
Alice
Tom
• Find the names of all employees who have
made sales greater than $1000.
SELECT emp_name FROM employees WHERE
emp_id = ALL (SELECT emp_id FROM sales
WHERE sale_amt > 1000);
emp_name
John
Mary
Bob

Tom
Aggregate Operators
• SQL aggregation function is used to perform
the calculations on multiple rows of a single
column of a table. It returns a single value.
• It is also used to summarize the data.
1. Count()

• The COUNT() function returns the number of


rows that matches a specified criterion.
• SELECT COUNT(column_name)
FROM table_name
WHERE condition;
2. AVG()
• The AVG() function returns the average value
of a numeric column.
• SELECT AVG(column_name)
FROM table_name
WHERE condition;
3. SUM()
• The SUM() function returns the total sum of a
numeric column.
• SELECT SUM(column_name)
FROM table_name
WHERE condition;
4. Min() and Max()
• The MIN() function returns the smallest value
of the selected column.
• The MAX() function returns the largest value
of the selected column.
• SELECT MIN(column name)
FROM table_name;
• SELECT MAX(column name)
FROM table_name;
ProductID ProductNam SupplierID CategoryID Quantity Price
e

1 Chais 1 1 12 18

2 Chang 1 1 30 19

3 Aniseed 1 2 25 10
Syrup

4 Chef Anton's 2 2 10 22
Cajun
Seasoning

5 Chef Anton's 2 2 3 21.35


Gumbo Mix
1. No. of Records or products from the
table:
• SELECT COUNT(ProductID)
FROM Products;
2. Average price of all products
• SELECT AVG(Price)
FROM Products;
3. Quantity of all products
• SELECT SUM(Quantity)
FROM Products;
4. Lowest product price
• SELECT MIN(Price)
FROM Products;
5. Highest Product price
• SELECT MAX(Price)
FROM Products;
NULL VALUES
• A field with a NULL value is a field with no
value.
• If a field in a table is optional, it is possible to
insert a new record or update a record
without adding a value to this field. Then, the
field will be saved with a NULL value.
How to Test for NULL Values?
• It is not possible to test for NULL values with
comparison operators, such as =, <, or <>.
• We will have to use the IS NULL and IS NOT
NULL operators instead.
IS NULL
• SELECT column_names
FROM table_name
WHERE column_name IS NULL;
IS NOT NULL
• SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
CustomerI CustomerN ContactNa Address City PostalCode Country
D ame me

1 Alfreds Maria Obere Str. Berlin 12209 Germany


Futterkiste Anders 57

2 Ana Ana Avda. de la México 05021 Mexico


Trujillo Trujillo Constitució D.F.
Empareda n 2222
dos y
helados

3 Antonio Antonio Mataderos México 05023 Mexico


Moreno Moreno 2312 D.F.
Taquería

4 Around Thomas 120 London WA1 1DP UK


the Horn Hardy Hanover
Sq.

Germany
• Update Customers
Set CustomerId=5, Name=“Geetha”,
ContactName=“Geetha”, Address=“Andhra”,
City=“Tirupathi”, postalcode=048393
Where customerId IS NULL;

You might also like