w3resource

SQL Exercises: Sort records using in operator


4. Customers with Specific IDs

From the following table, write a SQL query to retrieve the details of all customers whose ID belongs to any of the values 3007, 3008 or 3009. Return customer_id, cust_name, city, grade, and salesman_id.

Sample table: customer

 customer_id |   cust_name    |    city    | grade | salesman_id 
-------------+----------------+------------+-------+-------------
        3002 | Nick Rimando   | New York   |   100 |        5001
        3007 | Brad Davis     | New York   |   200 |        5001
        3005 | Graham Zusi    | California |   200 |        5002
        3008 | Julian Green   | London     |   300 |        5002
        3004 | Fabian Johnson | Paris      |   300 |        5006
        3009 | Geoff Cameron  | Berlin     |   100 |        5003
        3003 | Jozy Altidor   | Moscow     |   200 |        5007
        3001 | Brad Guzan     | London     |       |        5005

Sample Solution:

-- This query selects all columns from the 'customer' table.
SELECT *
-- Specifies the table from which to retrieve the data (in this case, 'customer').
FROM customer
-- Filters the rows to only include those where the 'customer_id' column has one of the values 3007, 3008, or 3009.
WHERE customer_id IN (3007, 3008, 3009);

Output of the Query:

customer_id	cust_name	 city		grade	salesman_id
3007		Brad Davis	 New York	200		5001
3008		Julian Green   London		300		5002
3009		Geoff Cameron	Berlin		100		5003

Code Explanation:

The said query in SQL that selects all columns (*) from a table called 'customer' where the "customer_id" is in the list of values (3007, 3008, 3009).
It will return a list of all the rows in the 'customer' table in which the 'customer_id' matches any of the specified values in the query.

Relational Algebra Expression:

Relational Algebra Expression: Sort records using in operator.

Relational Algebra Tree:

Relational Algebra Tree: Sort records using in operator.

Explanation:

Syntax of sort records using in operator

Visual presentation:

Result of sort records using in operator

Go to:


PREV : Salespeople Not in Paris or Rome.
NEXT : Salespeople with Commission 0.12-0.14.

For more Practice: Solve these Related Problems:

  • Write a SQL query to retrieve the details of customers whose ID belongs to any of the values 3001, 3002, or 3003. Return customer_id, cust_name, city, grade, and salesman_id.
  • Write a SQL query to find the details of customers whose ID does not belong to 3007, 3008, or 3009. Return customer_id, cust_name, city, grade, and salesman_id.
  • Write a SQL query to retrieve the details of customers whose ID is greater than 3005. Return customer_id, cust_name, city, grade, and salesman_id.
  • Write a SQL query to list all customers whose ID is less than 3004. Return customer_id, cust_name, city, grade, and salesman_id.


Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.