DBMS Exp 4
DBMS Exp 4
4
AIM : Perform nested queries
THEORY :
SUBQUERY :
Definition: A subquery, also known as an inner query or nested query, is a query within
another SQL query. The subquery is executed first, and its result is used by the main (outer)
query.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name OPERATOR (SELECT column_name FROM table_name WHERE
condition);
Example :
Find all employees who earn more than the average salary in the company.
SELECT employee_name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
INTERSECT :
SELECT column_name(s) FROM table_name1
INTERSECT
SELECT column_name(s) FROM table_name2;
EXCEPT :
SELECT column_name(s) FROM table_name1
EXCEPT
SELECT column_name(s) FROM table_name2;
Example :
UNION: Get a list of all cities from both customers and suppliers.
SELECT city FROM customers
UNION
SELECT city FROM suppliers;
INTERSECT: Find common product IDs in both orders and returns.
SELECT product_id FROM orders
INTERSECT
SELECT product_id FROM returns;
EXCEPT: Find employees who have not been assigned to any project.
SELECT employee_id FROM employees
EXCEPT
SELECT employee_id FROM project_assignments;
SOME :
Definition : The SOME operator compares a value to each value in a list or subquery. It
returns TRUE if the comparison is TRUE for at least one value in the list or subquery.
Syntax :
SELECT column_name(s)
FROM table_name
WHERE column_name OPERATOR SOME (SELECT column_name FROM table_name WHERE
condition);
Example :
Find employees whose salary is greater than some of the salaries in the 'salaries' table.
SELECT employee_name
FROM employees
WHERE salary > SOME (SELECT salary FROM salaries WHERE department = 'HR');
ALL :
Definition : The ALL operator compares a value to all values in a list or subquery. It returns
TRUE if the comparison is TRUE for all values in the list or subquery.
Syntax :
SELECT column_name(s)
FROM table_name
WHERE column_name OPERATOR ALL (SELECT column_name FROM table_name WHERE
condition);
Example :
Find employees whose salary is greater than all salaries in the 'interns' table.
SELECT employee_name
FROM employees
WHERE salary > ALL (SELECT salary FROM interns);
IN :
Definition: The IN operator allows you to specify multiple values in a WHERE clause. It is
used to filter the result set to only include rows where the specified column matches any
value in the list or subquery.
Syntax :
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ..., valueN);
Example :
Find products that are available in either 'Electronics' or 'Appliances' category.
SELECT product_name
FROM products
WHERE category IN ('Electronics', 'Appliances');
EXISTS :
Definition: The EXISTS operator is used to check the existence of any record in a subquery. It
returns TRUE if the subquery returns one or more records.
Syntax :
SELECT column_name(s)
FROM table_name
WHERE EXISTS (SELECT column_name FROM table_name WHERE condition);
Example :
Find customers who have placed at least one order.
SELECT customer_name
FROM customers
WHERE EXISTS (SELECT order_id FROM orders WHERE orders.customer_id =
customers.customer_id);
TABLES :
QUERIES :
1)find the name of the youngest person.
2)display the date and location of the accident for the car license ‘M8300’.
7)find the car details which has participated in accident at location ‘mumbai’ but not at
‘dadar’)////use set operations
8 )display the name of person participated in accident at location ‘mumbai’, ‘thane’,
‘mulund’, ‘dadar’)//// use set operations
9)display the details of person whose age is 25 and has participated in accident having
damage amount greater than 2000//exist
10)find the location of an accident where a car having license number m234T participated?
CONCLUSION :
In this experiment, we used SQL to perform nested queries on a car accident database. We identified
the youngest driver, retrieved accident details by car license, and examined damage amounts for
participants. We employed various operators to filter data based on specific criteria and utilized set
operations to analyze car involvement across different locations. Additionally, the use of the `EXISTS`
operator allowed us to find individuals meeting certain conditions. These tasks demonstrated SQL's
capability to analyze and extract meaningful insights from real-world data.