w3resource

SQL Exercises: Select all columns that satisfy a specific condition

SQL Basic Select Statement: Exercise-10 with Solution.

From the following table, write a SQL query to find customers whose grade is 200. Return customer_id, cust_name, city, grade, 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 'grade' column has the value 200.
WHERE grade = 200;

Output of the Query:

customer_id	cust_name	 city	    grade	salesman_id
3007		Brad Davis	New York	200		5001
3005		Graham Zusi	California	200		5002
3003		Jozy Altidor    Moscow		200		5007

Code Explanation:

The said SQL query that selects all columns from the 'customer' table where the value of "grade" column is 200. The WHERE clause is used to filter the rows and only return the rows that match the condition specified in the WHERE clause.
For this case, it will return all columns of the rows from the 'customer' table with "grade" 200.

Relational Algebra Expression:

Relational Algebra Expression: Select all columns against a specified condition

Relational Algebra Tree:

Relational Algebra Tree: Select all columns against a specified condition

Explanation :

Explanation: Select all columns against a specified condition

Visual presentation :

Result of select all columns against a specified condition

Practice Online


Query Visualization:

Duration:

Query visualization of Select all columns against a specified condition - Duration

Rows:

Query visualization of Select all columns against a specified condition - Rows

Cost:

Query visualization of Select all columns against a specified condition - Cost

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: Select specified columns with where clause.
Next SQL Exercise: Order which will be delivered by a specified salesman.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/sql-exercises/sql-retrieve-exercise-10.php