w3resource

SQL Exercise: Display customer name, city, grade


Customers Sorted by ID

From the following table, write a SQL query to find all the customers. Sort the result-set by customer_id. Return cust_name, city, grade.

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 specific columns ('cust_name', 'city', 'grade') from the 'customer' table and orders the result set by 'customer_id' in ascending order.
SELECT cust_name, city, grade
-- Specifies the table from which to retrieve the data (in this case, 'customer').
FROM customer
-- Orders the result set by the 'customer_id' column in ascending order.
ORDER BY customer_id;

Output of the Query:

cust_name	city		grade
Brad Guzan	London	
Nick Rimando	New York	100
Jozy Altidor	Moscow		200
Fabian Johnson	Paris		300
Graham Zusi	California	200
Brad Davis	New York	200
Julian Green	London		300
Geoff Cameron	Berlin		100

Code Explanation:

The said query in SQL retrieves data from the 'customer' table and returns the columns cust_name, city, and grade.
The "ORDER BY customer_id" clause sorts the rows in the result set based on the values in the customer_id column.

Relational Algebra Expression:

Relational Algebra Expression: Display customer name, city, grade (according to the smallest  ID).

Relational Algebra Tree:

Relational Algebra Tree: Display customer name, city, grade (according to the smallest  ID).

Explanation :

Syntax of display the customer name, city and grade etc. and smallest customer ID will comes first

Visual presentation :

Result of display the customer name, city and grade etc. and smallest customer ID will comes first


Practice Online



Query Visualization:

Duration:

Query visualization of Display customer name, city, grade (according to the smallest ID) - Duration

Rows:

Query visualization of Display customer name, city, grade (according to the smallest ID) - Rows

Cost:

Query visualization of Display customer name, city, grade (according to the smallest ID) - Cost

 

For more Practice: Solve these Related Problems:

  • Write a SQL query to list customer names, city, and grade sorted by customer_id ascending, excluding customers with NULL grade.
  • Write a SQL query to select customer details sorted by customer_id and include only those whose city begins with 'N' or 'L'.
  • Write a SQL query to retrieve customer details sorted by customer_id, including only customers with even grade values.
  • Write a SQL query to display cust_name, city, and grade sorted by customer_id, converting all city names to uppercase.
  • Write a SQL query to output customer details sorted by customer_id with an additional computed column categorizing grade as 'High' if above 150 and 'Low' otherwise.

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

Previous SQL Exercise: Oldest orders first on the highest purchase amount.
Next SQL Exercise: Salesman details by smallest ID along with order date.

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.