SQL is a programming language used to manage data in a relational database management system (RDBMS). SQL has many useful features and functions to perform various operations on data, such as inserting, updating, deleting, and retrieving data.
One of the operations that is often done in SQL is to check whether certain data is already in the table or not. This is useful for preventing duplication of data, validating inputs, or specifying conditions for other queries.
There are several ways to check if data is already in a table, but one of the most common and efficient ways is to use the EXISTS clause. An EXISTS clause is a conditional clause that returns a TRUE or FALSE value based on whether a given subquery results in at least one row or not.
In this article, Bardimin will discuss how to use EXISTS clauses in SQL, what are the advantages and disadvantages, and how to compare them to other methods, such as using COUNT or JOIN. Bardimin will also provide some examples of using EXISTS in various scenarios and databases, such as MySQL.
Basic SQL EXISTS Syntax and Examples
The basic syntax for using EXISTS clauses in SQL is as follows:
SELECT column_name(s) FROM table_name WHERE EXISTS (subquery);
Here, subquery is a query that is inside parentheses and usually refers to the same table or another table that is connected to the main table. The subquery must return at least one column, but the value of that column is irrelevant because EXISTS only care whether any rows are generated or not.
Let’s look at a simple example of using EXISTS in SQL. Suppose we have a mahasiswa table that contains data about students at a university, as follows:
id | nama | jurusan | angkatan |
1 | Ali | Mathematics | 2019 |
2 | Mind | Physics | 2020 |
3 | Cici | Biology | 2019 |
4 | Dedi | Chemistry | 2020 |
5 | One | Mathematics | 2021 |
We also have a nilai table that contains data about students’ grades in various courses, such as the following:
id_mahasiswa | kode_mk | nilai |
1 | MA101 | 80 |
1 | MA102 | 85 |
2 | FI101 | 75 |
2 | FI102 | 70 |
3 | BI101 | 90 |
4 | KI101 | 95 |
5 | MA101 | 60 |
If we want to display student data that has values in the nilai table, we can use the EXISTS clause as follows:
SELECT * FROM mahasiswa m WHERE EXISTS ( SELECT 1 FROM nilai n WHERE m.id = n.id_mahasiswa );
The results of the above query are as follows:
id | nama | jurusan | angkatan |
1 | Ali | Mathematics | 2019 |
2 | Mind | Physics | 2020 |
3 | Cici | Biology | 2019 |
4 | Dedi | Chemistry | 2020 |
5 | One | Mathematics | 2021 |
The above query uses a subquery that returns a value of 1 for each row that meets the condition m.id =n.id_mahasiswa, which means that the student has a value in the nilai table. Because the subquery returns at least one row for each student, the EXISTS clause returns TRUE and that student is displayed in the query results.
Note that we can use any value in a subquery, even NULL or *, because that value does not affect EXISTS results. However, for performance reasons, it is recommended to use the simplest constant values, such as 1, 0, or NULL, since SQL does not need to process the actual column values.
Advantages and Disadvantages of SQL EXISTS
Using EXISTS clauses in SQL has several advantages and disadvantages, depending on the situation and the database used. Here are some of the advantages and disadvantages of EXISTS: