w3resource

SQL Exercises: Select specified columns with where clause

SQL Basic Select Statement: Exercise-9 with Solution.

From the following table, write a SQL query to locate salespeople who live in the city of 'Paris'. Return salesperson's name, city. 

Sample table: salesman

 salesman_id |    name    |   city   | commission 
-------------+------------+----------+------------
        5001 | James Hoog | New York |       0.15
        5002 | Nail Knite | Paris    |       0.13
        5005 | Pit Alex   | London   |       0.11
        5006 | Mc Lyon    | Paris    |       0.14
        5007 | Paul Adam  | Rome     |       0.13
        5003 | Lauson Hen | San Jose |       0.12

Sample Solution :

-- This query selects specific columns 'name' and 'city' from the 'salesman' table.
SELECT name, city
-- Specifies the table from which to retrieve the data (in this case, 'salesman').
FROM salesman
-- Filters the rows to only include those where the 'city' column has the value 'Paris'.
WHERE city = 'Paris';

Output of the Query:

name		city
Nail Knite	Paris
Mc Lyon		Paris

Code Explanation:

The said query in SQL that selects "name" and "city" columns from the 'salesman' table where the value of "city" column is 'Paris'. The WHERE clause is used to filter the rows and only return the rows that match the condition specified in the WHERE clause.
As a result, all the rows from the 'salesman' table with a "city" column of 'Paris' will be returned, along with their "name" and "city" columns.

Syntax of select specified columns with where clause

Visual presentation:

Result of select specified columns with where clause

Practice Online


Query Visualization:

Duration:

Query visualization of Select specified columns with where clause - Duration

Rows:

Query visualization of Select specified columns with where clause - Rows

Cost:

Query visualization of Select specified columns with where clause - Cost

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

Previous SQL Exercise: Identify distinct values in a table.
Next SQL Exercise: Select all columns that satisfy a specific condition.

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