EXPERIMENT 4
AIM: Perform Nested and Complex queries.
THEORY:
NESTED WHERE CLAUS
• Set membership :
Checks if a value is in a set of results returned by a subquery.
Syntax: WHERE column_name IN (SELECT column_name FROM table_name WHERE
condition)
Example: SELECT * FROM employees WHERE department_id IN (SELECT department_id
FROM departments WHERE location = 'New York');
• 'some' clause:
Compares a value to a set of values returned by a subquery, returning true if the condition is met
for at least one row.
Syntax: WHERE column_name operator SOME (SELECT column_name FROM table_name
WHERE condition)
Example: SELECT * FROM products WHERE price > SOME (SELECT price FROM
competitor_products WHERE category = 'Electronics');
• 'all' clause:
Compares a value to a set of values returned by a subquery, returning true if the condition is met
for all rows.
Syntax: WHERE column_name operator ALL (SELECT column_name FROM table_name
WHERE condition)
Example: SELECT * FROM employees WHERE salary > ALL (SELECT avg_salary FROM
department_averages);
• EXISTS:
Checks if the subquery returns any rows, returning true if it does.
Syntax: WHERE EXISTS (SELECT column_name FROM table_name WHERE condition)
Example: SELECT * FROM customers WHERE EXISTS (SELECT * FROM orders WHERE
orders.customer_id = [Link]);
• UNIQUE:
Checks if the subquery returns only unique (distinct) rows.
Syntax: WHERE UNIQUE (SELECT column_name FROM table_name WHERE condition)
Example: SELECT * FROM products WHERE UNIQUE (SELECT order_id FROM
order_details WHERE order_details.product_id = [Link]);
• Subqueries in the FROM clause:
Uses a subquery as a derived table in the FROM clause.
Syntax: SELECT column_name FROM (SELECT column_name FROM table_name WHERE
condition) AS alias
Example: SELECT avg_salary FROM (SELECT AVG(salary) as avg_salary FROM employees
GROUP BY department_id) AS dept_averages;
• WITH clause:
Defines named subqueries that can be referenced multiple times in the main query.
Syntax: WITH alias_name AS (SELECT column_name FROM table_name WHERE condition)
Example: WITH high_salary_employees AS (SELECT * FROM employees WHERE salary >
100000) SELECT department_id, COUNT(*) FROM high_salary_employees GROUP BY
department_id;
• Complex query using WITH clause:
Uses multiple named subqueries to break down a complex query into simpler, more readable
parts.
Syntax: WITH alias1 AS (subquery1), alias2 AS (subquery2) SELECT ... FROM alias1, alias2
WHERE ...
Example: WITH dept_totals AS (SELECT department_id, SUM(salary) as total_salary FROM
employees GROUP BY department_id), high_salary_depts AS (SELECT department_id FROM
dept_totals WHERE total_salary > 1000000) SELECT e.* FROM employees e JOIN
high_salary_depts h ON e.department_id = h.department_id;
• Subquery in the SELECT clause (scalar subquery):
Uses a subquery that returns a single value in the SELECT clause.
Syntax: SELECT column_name, (SELECT aggregate_function(column_name) FROM
table_name WHERE condition) AS alias FROM table_name
Example: SELECT employee_name, (SELECT AVG(salary) FROM employees WHERE
department_id = e.department_id) AS dept_avg_salary FROM employees e;
Case study 2: Employee database
Create table for given schema
employee (employee_id, employee name,age, city,state)
works (employee_id, company name, salary)
company (Company_name, city)
manages (employee_id, mgr_id,company_name)
Table - employee
Table - works
Table - Company
Table - manages
1. Display the name of the employee with the highest salary.
2. Retrieve all the details of the Manager who manages “TCS” company.
3. Display the details of all employees who receive more than average salary.
4. Return the name of the employees whose salary is more than the salary of all the
employees working in “TCS”.
5. Display the names of Companies located in “Mumbai”,”Delhi” Or “Calcutta”
6. Display the name of companies located at “Mumbai”,”Banglore”,”Chennai” and
“Pune”
7. Retrieve the employee details who is older than all the employees working in
“Mumbai”.
8. Retrieve the details of an employee who is working in “TCS” company and
getting salary more than all the employees of the “L & T” company.
9. Find the Company names which are located in “Mumbai” but not in “Delhi”.
10. Find the location of the Company whose employees are getting minimum salary.
11. Find details of the employee who are from Pune or pune or Nasik (Use set
operations)
12. Find employee id whose salary is greater than 10K and he is from Pune.(Use Set
Operation)
13. Find an employee id whose salary is greater than 10K but he is not from
Pune.(Use Set Operation)
CONCLUSION:
Learnt to perform basic nested queries in FROM clause, WHERE clause and
SELECT clasue and complex queries using WHERE clause. Learnt function of
some and all clauses, test for empty relations using EXISTS clause. Became more
familiar with set operations intersect, union and except.