Open In App

MariaDB Subqueries

Last Updated : 29 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

subquery

Subquery

Types 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:

rowSubquery

Row Subquery

2. 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:

scalarSubquery

Scalar Subquery

3. 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:

TableSubquery

Table Subquery

4. 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.

SubqueryWHERE

Subquery with WHERE Clause

5. 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.

CorrelatedSubquery

Correlated Subquery

6. 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.

SubqueryIN

Subquery with IN Operator

7. 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.

SubqueryExists

Subquery with EXISTS Operator

Conclusion

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads