Docsity

MySQL Subqueries: A Comprehensive Guide, Study notes of Database Management Systems (DBMS)

Partial preview of the text

Download MySQL Subqueries: A Comprehensive Guide and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

MySQL Subquery

โ–ช A subquery in MySQL is a query, which is nested into another SQL query and embedded with SELECT, INSERT, UPDATE or DELETE statement along with the various operators. โ–ช We can also nest the subquery with another subquery. A subquery is known as the inner query , and the query that contains subquery is known as the outer query. โ–ช The innerquery executed first gives the result to the outer query, and then the main/outer que ry willbe performed. โ–ช MySQL allows us to use subquery anywhere, but it must be closed within parenthesis.

The following are the advantages of using subqueries:

  • The subqueries make the queries in a structured form that allows us to isolate each part of a statement.
  • The subqueries provide alternative ways to query the data from the table; otherwise, we need to use complex joins and unions.
  • The subqueries are more readable than complex join or union statements. MySQL Subquery Syntax SELECT column_list (s) FROM table_name WHERE column_name OPERATOR ( SELECT column_list (s) FROM table_name [ WHERE ])

MySQL Subquery Example

MySQL Subquery with Comparison Operator A comparison operator is an operator used to compare values and returns the result, either true or false. The following comparison operators are used in MySQL <, >, =, <>, <=>, etc. We can use the subquery before or after the comparison operators that return a single value. The returned value can be the arithmetic expression or a column function. After that, SQL compares the subquery results with the value on the other side of the comparison operator. Find an employee detail whose income is more than 350000 with the help of subquery:

SELECT * FROM employees WHERE emp_id IN ( SELECT emp_id FROM employees WHERE income > 350000);

MySQL Subquery with IN or NOT-IN Operator

If the subquery produces more than one value, we need to use the IN or NOT IN operator with the WHERE clause. Suppose we have a table named "Student" and "Student2" that contains the following data: Table: Student Table: Student

Find student detail who does not belong to Los Angeles City SELECT Name , City FROM student WHERE City NOT IN ( SELECT City FROM student2 WHERE City='Los Angeles' ); After execution, we can see that the result contains the student details not belonging to Los Angeles City.

It will give the output as follows:

MySQL Correlated Subqueries A correlated subquery in MySQL is a subquery that depends on the outer query. It uses the data from the outer query or contains a reference to a parent query that also appears in the outer query. MySQL evaluates it once from each row in the outer query. SELECT emp_name, city, income FROM employees emp WHERE income > ( SELECT AVG(income) FROM employees WHERE city = emp.city); In the above query, we select an employee name and city whose income is higher than the average income of all employees in each city.

MySQL Subqueries with EXISTS or NOT EXISTS โ– The EXISTS operator is a Boolean operator that returns either true or false result. โ– It is used with a subquery and checks the existence of data in a subquery. โ– If a subquery returns any record at all, this operator returns true. Otherwise, it will return false. โ– The NOT EXISTS operator used for negation that gives true value when the subquery does not return any row. Otherwise, it returns false. โ– Both EXISTS and NOT EXISTS used with correlated subqueries.

Suppose we have a table customer and order that contains

the data as follows:

MySQL ROW Subqueries

It is a subquery that returns a single row where we can get more than one column values. We can use the following operators for comparing row subqueries =, >, <, >=, <=, <>, !=, <=>. SELECT * FROM customer C WHERE ROW(cust_id, occupation) = ( SELECT order_id, order_date FROM Orders O WHERE C.cust_id = O.cust_id); If given row has cust_id, occupation values equal to the order_id, order_date values of any rows in the first table, the WHERE expression is TRUE, and each query returns those first table rows. Otherwise, the expression is FALSE, and the query produces an empty set