0% found this document useful (0 votes)
11 views37 pages

1742880544_Subqueries_in_SQL

The document discusses various SQL queries and concepts, focusing on subqueries, aggregate functions, and derived queries. It provides examples of how to retrieve customer information, order details, and perform calculations like averages and counts. Additionally, it explains the use of keywords like EXISTS, IN, and aggregate functions in SQL queries.

Uploaded by

TANISHA SINHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views37 pages

1742880544_Subqueries_in_SQL

The document discusses various SQL queries and concepts, focusing on subqueries, aggregate functions, and derived queries. It provides examples of how to retrieve customer information, order details, and perform calculations like averages and counts. Additionally, it explains the use of keywords like EXISTS, IN, and aggregate functions in SQL queries.

Uploaded by

TANISHA SINHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Subqueries in SQL

Query: What are the name and address of the


customer who placed order number 1008?
Query: What are the name and address of the
customer who placed order number 1008?
Think & Tell !!
Does the value for OrderID appear in the query result ??
Think & Tell !!
What will happen if an Order with order number 1008 does not exist??
Query: What are the names of customers who
have placed orders?

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 ??

EXISTS is used to test whether any rows fit the


conditions, and not to return values from
particular columns.
True or False ??

We use EXISTS (NOT EXISTS) when our only interest


is whether the subquery returns a nonempty (empty)
set (i.e., we don’t care what is in the set, just
whether it is empty or not)
True or False ??

We use IN (NOT IN) when we need to know what


values are (are not) in the set.
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

Takes a set of values as input and


returns a single value as output.

Can u Guess
any Example!!
Aggregate Functions

Takes a set of values as input and


returns a single value as output.
Average: Minimum: Maximum:
avg min max

Count:
Total: sum
count
Query: Find average age of students in
Management Studies Department

Select avg (Student_age)


From Students
Where Dept_name = ‘Management Studies’;
Query: Find average age of students in
Management Studies Department

Select avg (Student_age) as avg_student_age What is


From Students the
Where Dept_name = ‘Management Studies’; difference
here ??
Query: Find the total number of faculties who
teach a course in Winter Semester 2025

Select count (distinct Faculty_ID)


From Faculty
Where Semester =‘Winter’ and year = 2025; What is the
Interpretation ??
Be very specific
!!
Query: Find the average salary of staff in each
department
Staff Table

Select dept_name, avg(salary) as avg_salary


From Staff
Group by dept_name;

Write down the OUTPUT ??


OUTPUT
Query: Find all the courses taught in the Fall Semester
2009 but not in Spring Semester 2010

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);

Write down the OUTPUT ??


Query: Find the total number of distinct students who
have taken courses taken by instructor with ID 10101

Try the Code !!


Query: Find the total number of distinct students who
have taken courses taken by instructor with ID 10101

Select count (distinct ID)


From takes
Where (course_id, sec_id, semester, year) in (select course_id, sec_id, semester, year
from teaches
where teaches.ID= 10101);
Query: Find the names of all instructors that have a
salary value greater than that of each instructor in the
Biology Department

Try the Code !!


Query: Find the names of all instructors that have a
salary value greater than that of each instructor in the
Biology Department

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

Try the Code !!


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

Try the Code !!


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.

Why there was need for Derived Tables ??

You might also like