w3resource

SQL Exercise: Display customer name, city and grade by highest grade

SQL Formatting Output: Exercise-8 with Solution

From the following table, write a SQL query to find all the customers. Sort the result-set in descending order on 3rd field. Return customer name, city and 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.
-- It orders the result set by the third column ('grade') in descending order using the column index.
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 third column ('grade') in descending order using the column index (3).
ORDER BY 3 DESC;

Output of the Query:

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

Code Explanation:

The said query in SQL that retrieves the customer name, city, and grade from the 'customer' table and orders the result set by the "grade" column in descending order. The result will show the customer name, city, and grade with the highest grades appearing first in the result set.

Relational Algebra Expression:

Relational Algebra Expression: Display customer name, city and grade by highest grade.

Relational Algebra Tree:

Relational Algebra Tree: Display customer name, city and grade by highest grade.

Explanation:

Syntax of display customer name, city and grade in such a manner that, the customer holding highest grade will comes first

Visual presentation :

Result of display customer name, city and grade in such a manner that, the customer holding highest grade will comes first

Practice Online


Query Visualization:

Duration:

Query visualization of Display customer name, city and grade by highest grade - Duration

Rows:

Query visualization of Display customer name, city and grade by highest grade - Rows

Cost:

Query visualization of Display customer name, city and grade by highest grade - Cost

 

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

Previous SQL Exercise: Salesman details by smallest ID along with order date.
Next SQL Exercise: Find largest number of orders booked by the customer.

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-formatting-output-exercise-8.php