![]() |
Subqueries and EXISTS are powerful tools in MariaDB that enable us to write complex and efficient queries. Subqueries allow us to nest one query inside another and provide a flexible way to retrieve data. The EXISTS operator, on the other hand, it checks for the existence of rows returned by a subquery. In this article, we will look at what subqueries are, how they work, and how the EXISTS operator can be used to extend their functionality in MariaDB. What are Subqueries?A subquery, commonly referred to as a nested or inner query, is a query within another SQL statement, such as SELECT, INSERT, UPDATE, and DELETE that sometimes references an outer query. Rather, a subquery’s outcome can be utilized in different parts of the main query including the WHERE clause, FROM clause, or another subquery. Syntax: SELECT column1 FROM table1 WHERE column2 = (SELECT column3 FROM table2 WHERE condition); Explanation: In this syntax, the subquery (SELECT column3 FROM table2 WHERE condition) is enclosed within parentheses and returns a single value. This value is then compared to column2 in the outer query’s WHERE clause. What is an EXISTS Operator?The EXISTS operator makes the subquery returns only the records that are present in the result set of the of query. It will return TRUE if the sub-query has one or more rows, if not it will return FALSE. EXISTS operator is regularly employed in conjunction with correlated sub-queries, where the first query is related to outer columns. Syntax: SELECT column1 FROM table1 WHERE EXISTS (SELECT * FROM table2 WHERE table2.column = table1.column); Explanation: In the above Syntax, the EXISTS operator checks whether there are any rows in table2 that match the condition specified in the subquery. If at least one row exists, the outer query returns true. Examples of Subqueries with Exists OperatorHere are the some examples of using subqueries with exists operator: Let’s first create table and insert values into it. Create table customers: CREATE TABLE customers ( Insert data: INSERT INTO customers (name, age) VALUES Output: ![]() Customers Table Explanation: As we can see our table got created successfully. Create table orders. CREATE TABLE orders( Insert data: INSERT INTO orders (customers_id, order_date, total_amount) VALUES Output: ![]() Orders Table Explanation: As we can see table got created successfully. Example 1: Using EXISTS to Find Customers Older Than 30.This query provides the names of customers who have placed orders but are more than 30 years old. Query: SELECT NAME FROM customers c WHERE EXISTS Output: ![]() Example 1 Explanation:
Example 2: Using EXISTS to Find Customers Whose Name Starts with ‘M’It finds the customers’ names that have the first letter ‘M’ and who has at least one other customers with the same name beginning with ‘M’. Query: SELECT name FROM customers u WHERE EXISTS Output: ![]() Example 2 Explanation:
Example 3: Using EXISTS to Find Customers Who have Made Orders with a Total Amount Greater Than 100.This SQL query fetches names of customers who have made orders with a total sum greater than $100. Query: SELECT name FROM customers u WHERE EXISTS Output: ![]() Example 3 Explanation:
Example 4: Using EXISTS to Find Customers Who have Made Orders in January 2024.This query will only fetch those clients whose orders were placed in January 2024. Query: SELECT name FROM customers u WHERE EXISTS Output: ![]() Example 4 Explanation:
Advantages of Subqueries and EXISTS Operator
ConclusionSubqueries and EXISTS operator are important parts of MariaDB which allow developers to write powerful SQL queries. Through the knowledge of how to invoke subqueries and EXISTS you can make your database operations easier to read and more flexible in addition to faster. Practice with the presented ideas in your own projects in order to gain their full advantage and improve your SQL queries. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |