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


Sample Solution:

SELECT cust_name,city,grade 
FROM customer 
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.



Follow us on Facebook and Twitter for latest update.

SQL: Tips of the Day

How to request a random row in SQL?

Select a random row with MySQL:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

Select a random row with PostgreSQL:

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

Select a random row with Microsoft SQL Server:

SELECT TOP 1 column FROM table
ORDER BY NEWID()

Select a random row with IBM DB2:

SELECT column, RAND() as IDX 
FROM table 
ORDER BY IDX FETCH FIRST 1 ROWS ONLY

Select a random record with Oracle:

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

Database: SQL Server, PostgreSQL Server, MySQL

Ref: https://bit.ly/39n35HP

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook