MariaDB utilizes SQL, a structured query language, as an open-source Relational database management system (RDBMS). It manages and manipulates data efficiently. One powerful function that contributes to the flexibility of MariaDB is using subqueries. This article will focus on MariaDB subqueries. We will explore their syntax, types, and various examples of subqueries.
What is a Subquery in MariaDB
A subquery, additionally known as an inner question or nested query, is a query nested within every other SQL statement. The result of a subquery may be used as a circumstance or expression within the essential question. This functionality enhances the querying abilities of MariaDB, taking into account more complicated and dynamic data retrieval.
SubqueryTypes of Subqueries in MariaDB
1. Row Subquery
A row subquery returns a single row of data. In this query, we are finding the employees whose salary is greater than the salary of Maram.
SELECT first_name, salary FROM employees WHERE (salary, department_id) > (SELECT salary, department_id FROM employees WHERE first_name = 'Maram');
The output will include the first name and salary of the employees who have higher salary and department id than the employee with the first name Maram. In the given example, the Vamshi, Sukumar, Kavya and Mahi have the higher salary and department id from Maram.
Output:
Row Subquery2. Scalar Subquery
A scalar subquery returns a single column from a row. In the below query we will retrieving the highest salary from the employees table and using it to get employees earning more than that amount.
SELECT first_name, salary FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
The results will include the first name and salaries of employees whose salaries are less than the highest salary in the entire employee table. The result will include all employees whose salary is not equal to or greater than the maximum salary in the table.
Output:
Scalar Subquery3. Table Subquery
Table subquery returns multiple rows as well as columns. In the below given query , we are using a table subquery to find employees from the department with the highest average salary.
SELECT first_name, salary FROM employees WHERE department_id = (SELECT department_id FROM (SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id ORDER BY avg_salary DESC LIMIT 1) AS subquery);
The output will include the first name and salary of employees. They work in the department with the highest average salary in the employees table. If there are multiple departments with the same highest average salary, the LIMIT 1 ensures that only one department is selected.
Output:
Table Subquery4. Subquery with WHERE Clause
This subquery selects the first name, last name, employee id and salary from the employees table and calculates the average salary for each group. The HAVING clause filtter out the groups where the average salary is less than 55000.
SELECT first_name, last_name, employee_id, salary FROM employees WHERE employee_id IN (SELECT employee_id FROM employees GROUP BY employee_id HAVING AVG(salary) > 55000);
Output:
The result of this query might be all columns for every row inside the employee table where the income is greater than 55000, and there exists every other record with a non-null Hire_date for the equal employee_id.
Subquery with WHERE Clause5. Correlated Subquery
A correlated subquery is a subquery that refers to columns of the outer query in its WHERE clause. It is dependent on outer query. This query is retrieving the first name from the employees table whose department_id matches the department_id of the employee with the first name Maram.
SELECT first_name FROM employee e1 WHERE department_id = (SELECT department_id FROM employee WHERE first_name = 'Maram');
Output:
The output of this query would be the first names of the employees who belong to the same department as the employee named 'Maram' in the employees table.
Correlated Subquery6. Subquery with IN Operator
SELECT * FROM employee WHERE department_id IN (101, 102);
This query retrieves all columns for rows in the employee table where the department_id is either 101 or 102. The IN clause is used to specify a list of values for which the query will return matching rows.
Output:
The output of this query will be all columns for each row in the employee table where the department_id is either 101 or 102. The output includes all information (all columns) for employees in the specified departments.
Subquery with IN Operator7. Subquery with EXISTS Operator
This query selects all columns where the salary is greater than 55000, and there exists another record in the same table with a non-null Hire_date for the same employee_id. The EXISTS clause is used to check for the existence of such records.
SELECT * FROM employee e WHERE salary > 55000 AND EXISTS (SELECT 1 FROM employee WHERE employee_id = e.employee_id AND Hire_date IS NOT NULL);
Output:
The result of this query will be all columns for each row in the "employee" table where the salary is greater than 55000, and there exists another record with a non-null Hire_date for the same employee_id.
Subquery with EXISTS OperatorConclusion
In conclusion, MariaDB subqueries provide a powerful and flexible way to enhance the database's querying capabilities. The ability to nest queries within other SQL statements allows for more dynamic and context-aware data retrieval. This article explored various types of subqueries in MariaDB. It included row, scalar, and table subqueries. The examples provided demonstrated how to use subqueries in different scenarios. For example, filtering data based on conditions, performing calculations, and utilizing correlated subqueries. We also covered subqueries with the IN and EXISTS operators. This showed how they effectively handle complex conditions.
Similar Reads
SQL Correlated Subqueries
In SQL, correlated subqueries are powerful tools that allow us to perform row-by-row comparisons and retrieve complex data. Unlike regular subqueries, correlated subqueries depend on values from the outer query, making them dynamic and highly effective for solving complex database problems.In this a
5 min read
PL/SQL Subqueries
PL/SQL subqueries are powerful SQL features that allow for the nesting of one query inside another for dynamic data retrieval. They have extensive applications when complex problems are to be solved by reducing them into smaller, more manageable queries. The inner query, usually called the subquery,
9 min read
SET Variable in MariaDB
In MariaDB, the SET statement is a main tool in variable handling. Users can assign values to variables, operate with them, and control database operations in various respects. This article includes a look at the SET variable usage in MariaDB and its syntax together with some examples. SET Variable
4 min read
MariaDB Procedure
MariaDB is an open-source database to help store and organize data. Itâs similar to other popular database software like MySQL but has some unique features and improvements. The MariaDB database is used for various purposes such as data warehousing, e-commerce, enterprise-level features, and logging
5 min read
MariaDB Unique Index
MariaDB is a fast, scalable, open-source community-supported relational database management system thatâs also an enhanced version of MySQL. Content management systems (CMS) are a key application of MariaDB. A CMS is a publication system through which web creators can push and manage large quantitie
5 min read
Show Tables in MariaDB
MariaDB is an open-source relational database management system (RDBMS). MariaDB is a very successful RDBMS that is known for its performance, scalability, and ease of use. When dealing with databases, understanding the structure and organization of their table type becomes important. MariaDB provid
4 min read
MariaDB Create Triggers
Triggers are a very useful and powerful feature of MariaDB. It is a database object associated with a table that activates if an INSERT, UPDATE or DELETE operations are performed. The name itself reflects their action as they run immediately without any human intervention when the respective operati
6 min read
MariaDB Drop Trigger
In a database management system(DBMS), triggers are essential for automating actions based on specific events. However, there are times when certain triggers need to be removed or dropped from the database. In MariaDB, the DROP TRIGGER statement provides a straightforward way to achieve this task. I
4 min read
MariaDB Drop View
MariaDB is an open-source relational database management system that is based on SQL(Structured query language). It is an improved version of MySQL and has various features, security, and performance when compared to MySQL. This database is open source with a strong community that can be trusted in
4 min read
MYSQL Subquery
A subquery is embedded inside another query and acts as input or output for that query. Subqueries are also called inner queries and they can be used in various complex operations in SQL. Subqueries help in executing queries with dependency on the output of another query. Subqueries are enclosed in
4 min read