The TOP clause in SQL is used to fetch the limited result from the table. It is often used when dealing with larger databases and tables.
- Syntax of TOP Clause –
SELECT TOP value
column1,column2,...,columnN
FROM table_name
WHERE condition ;
OR,
- Syntax of TOP (percent) Clause –
SELECT TOP value PERCENT column1,column2,...,columnN
FROM table_name
WHERE condition;
Demo table –
Product Table
Category |
Region |
City |
Country |
Customer_ID |
Customer_name |
Office Supplies |
West |
Chicago |
United States |
SM-20950 |
Suzanne McNair |
Technology |
Central |
New York City |
United States |
AH-10465 |
Amy Hunt |
Office Supplies |
East |
New York City |
United States |
AH-10465 |
Amy Hunt |
Office Supplies |
East |
Dover |
United States |
EP-13915 |
Emily Phan |
Office Supplies |
East |
Dover |
United States |
EP-13915 |
Emily Phan |
Technology |
East |
Aurora |
United States |
TP-21565 |
Tracy Poddar |
Office Supplies |
West |
Aurora |
United States |
TP-21565 |
Tracy Poddar |
Office Supplies |
West |
Long Beach |
United States |
AR-10825 |
Anthony Rawles |
Office Supplies |
East |
Pasadena |
United States |
HA-14920 |
Helen Andreada |
Furniture |
West |
Pasadena |
United States |
HA-14920 |
Helen Andreada |
Example – Using the TOP CLAUSE, retrieve the top 2 from the demo table where the Region is “West”.
SELECT TOP 2 * FROM [dbo].[Products] WHERE Region = 'West';
The output after implementing the above TOP CLAUSE –
Category |
Region |
City |
Country |
Customer_ID |
Customer_name |
Technology |
West |
Aurora |
United States |
TP-21565 |
Tracy Poddar |
Office Supplies |
West |
Aurora |
United States |
TP-21565 |
Tracy Poddar |
Example – Using the TOP PERCENT CLAUSE, retrieve the top 50% records from the demo table where the Region is “West”.
SELECT TOP 50 PERCENT * FROM [dbo].[Products] WHERE Region = 'West';
The output after implementing the above TOP PERCENT CLAUSE –
Category |
Region |
City |
Country |
Customer_ID |
Customer_name |
Technology |
West |
Aurora |
United States |
TP-21565 |
Tracy Poddar |
Office Supplies |
West |
Aurora |
United States |
TP-21565 |
Tracy Poddar |
Office Supplies |
West |
Pasadena |
United States |
HA-14920 |
Helen Andreada |
Furniture |
West |
Pasadena |
United States |
HA-14920 |
Helen Andreada |
Office Supplies |
West |
Mesa |
United States |
HJ-14875 |
Heather Jas |
Other SQL topics to check out:
Follow us on Twitter, Facebook, Linkedin, and Tableau Public to stay updated with our latest blog and what’s new in Tableau.
If you are looking forward to getting your data pipeline built and setting up the dashboard for business intelligence, book a call now from here.
#analytics #data #business #artificialintelligence #machinelearning #startup #deeplearning #deeplearning #datascience #ai #growth #dataanalytics #india #datascientist #powerbi #dataanalysis #tableau #SQL #businessanalytics #businessanalyst #businessandmanagement #dataanalyst #businessanalysis #analyst #analysis #powerbideveloper #powerbidesktop #letsviz