1742880544_Subqueries_in_SQL
1742880544_Subqueries_in_SQL
The qualifiers NOT, ANY, and ALL may be used in front of IN or with logical operators
such as = , >, and <.
“= “ or “IN”
“= “ or “IN”
“= “ or “IN”
“= “ or “IN”
Query: Which customers have not placed any
orders for computer desks?
Subqueries with EXISTS
Query: What are the order IDs for all orders that have included furniture
finished in natural ash?
“EXISTS” or “IN”
“EXISTS” or “IN”
True or False ??
IN and NOT IN return a set of values from only one column, which
can then be compared to one column in the outer query.
True or False ??
EXISTS and NOT EXISTS return only a true or false value depending
on whether there are any rows in the answer table of the inner query
or subquery.
Aggregate Functions
Can u Guess
any Example!!
Aggregate Functions
Count:
Total: sum
count
Query: Find average age of students in
Management Studies Department
Refer to the
Given Tables
Select distinct course_id
From section
Where semester = ‘Fall’ and year = 2009 and
Course_id not in (select course_id
from section
where semester = ‘Spring’ and year = 2010);
Select name
From instructor
Where salary > all (select salary
from instructor
where dept_name =‘Biology’);
Query: Find the names of all instructors whose salary is
greater than at least one instructor in the Biology
Department
Select name
From instructor
Where salary > some (select salary
from instructor
where dept_name =‘Biology’);
Query: Find the department that have the highest
average salary
Select dept_name
From instructor
Group by dept_name
Having avg (salary) > = all (select avg (salary)
from instructor
group by dept_name);
Derived Queries
• Creating a derived table that has an aggregate value in it,
such as MAX, AVG, or MIN, allows the aggregate to be
used in the WHERE clause.
Query: Show product description, product standard price, and
overall average standard price for all products that have
standard price that is higher than average standard price.