w3resource

SQL Exercise: Display customer name, city, grade

SQL Formatting Output: Exercise-6 with Solution

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


Sample Solution:

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

 

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.

SQL: Tips of the Day

A table name as a variable -

For static queries, like the one in your question, table names and column names need to be static.

For dynamic queries, you should generate the full SQL dynamically, and use sp_executesql to execute it.

Static query:

SELECT * FROM [DB_ONE].[dbo].[ACTY]
EXCEPT
SELECT * FROM [DB_TWO].[dbo].[ACTY]

Ref: https://bit.ly/3sFxxnk

 





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