SQL SELECT Statements

The SELECT Statements are most often used in the Structured Query Language (SQL). This statement is used to retrieve data from the databases. It also retrieves the data from one or more databases where any sort of conditions are given. The SELECT Statement allows users to access a particular record from a specified column of the table.

The data retrieved after performing the SELECT Statement are stored in the result table and called the result- set.

  • Syntax of SELECT Statement –

SELECT column1,column2,column3,...,columnN FROM table_name ;

The columns shown in the syntax are the field names of the table from where we want to select data.

Suppose, If the user wants to select all the fields available in the table, it can use the following syntax:

SELECT * FROM table_name;

The (*) icons represent the selection of all fields in the syntax.

Example – Let’s test the above syntax using the PRODUCT table from the Customer Database –

SELECT * FROM [dbo].[Products];

The output after executing the above statement is as shown –

Category Region City Country Customer_ID Customer_name
Office Supplies Central Chicago United States SM-20950 Suzanne McNair
Technology East 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 West Aurora United States TP-21565 Tracy Poddar
Office Supplies West Aurora United States TP-21565 Tracy Poddar
Office Supplies East Long Beach United States AR-10825 Anthony Rawles
Office Supplies West Pasadena United States HA-14920 Helen Andreada
Furniture West Pasadena United States HA-14920 Helen Andreada

Example to SELECT Columns –

Consider the Product table of the Customer database and select Category, and Region columns from the table.

SELECT Category,Region FROM [dbo].[Products];

 

The output after executing the above statement is as shown –

Category Region
Office Supplies Central
Technology East
Office Supplies East
Office Supplies East
Office Supplies East
Technology West
Office Supplies West
Office Supplies East
Office Supplies West
Furniture West

 

  • SELECT DISTINCT Statement –

In the database tables, the values entered can contain some duplicate values, so to include only distinct values, we can add the DISTINCT keyword with the SELECT Statement. The DISTINCT Keyword shows only the distinct values of the columns parsed in the statement.

  • Syntax of SELECT – DISTINCT Statement –

SELECT DISTINCT column1,column2,column3,...,columnN FROM table_name ;

Example for SELECT – DISTINCT Statement –

Consider the Product table of the Customer database and select the distinct Category from the table.

SELECT DISTNCT Category FROM [dbo].[Products];

 

The output after executing the above statement is as shown –

Category
Office Supplies
Technology
Furniture

 

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.