DATA with BARAA

SQL EXISTS & IN

In this tutorial, you will learn how to embed a query within another query using the SQL EXISTS and IN.

Once you know how to fetch your data usingĀ  SELECT andĀ FROM and the next step is to learn how to filter your data using WHERE clause.

In the previous tutorial, you learned how to retrieve all your rows from tables, but in real-world scenarios, we usually select only the rows which fulfill certain conditions like customers who come from a certain country.

Syntax

The basic syntax of the WHERE clause to filter the data returned by a query can be given with:

				
					SELECT column_names
FROM table_name
WHERE EXISTS 
      (SELECT column_name
       FROM table_name
       WHERE condition)
				
			
				
					SELECT column_names
FROM table_name
WHERE column_name IN 
      (SELECT column_name
       FROM table_name
       WHERE condition)
				
			

It’s real easy to read in a plain-English way: select some columns from a table, except that the results will only include rows where that fulfill my conditions.

  • The clauses need to be in this order e SELECT Ā FROM WHERE
  • The WHERE clauseĀ  appears immediately after theĀ FROMĀ clause
  • In WHERE clause, you can specify one or more comparisons and logical operators.

Examples

To understand the ORDER BY statement in a better way, let’s look at the following customersĀ and orders tables in our tutorial database:

Now, let’s check out some examples that demonstrate how it actually works.

We have the following task be to solve using SQL statements

Find all orders placed from customers whose score higher than 500 using customer ID

The following SQL statement will returns all customers from customers table and order the result by the score column in ascending order.

You can have the same the result set by skipping the ASC, because it is the default option in ORDER BY.

				
					SELECT *
FROM orders
WHERE customer_id IN
( SELECT customer_id
  FROM customers
  WHERE score > 500)
				
			
				
					SELECT *
FROM orders AS o
WHERE EXISTS
( SELECT 1
  FROM customers AS c
  WHERE c.customer_id = o.customer_id
  AND c.score > 500 )
				
			
.

After executing the above query, you’ll get the result set something like this:

As you can see the output contains everything the whole customers tables including all rows and columns.

Similarly, you can use the DESC option to perform a sorting in descending order. The following statement will orders the result set by the numeric salary column in descending order.

Find all customers whose score is greater than 500
				
					SELECT
    *
FROM customers
WHERE score > 500
				
			

Once you specify multiple columns after ORDER BY, the Database will sort the result by the first column, then the new ordered list will be sorted again by next column.

Operators in WHERE Clause

You can filter your results in a number of ways using comparison and logical operators, which you’ll learn about in the next tutorials. I summarized in the following table the most important ones.

.
Share it !