SELECT

The SQL SELECT Statement

The SELECT statement is used to read data from a database. The data returned is called the result set.

Syntax

SELECT column1, column2, ...
FROM table_name;

Here, column1, column2, ... are the column names you want. To read every column, use *:

SELECT * FROM table_name;

Select Specific Columns

Example

Return only the name and city of every customer:

SELECT CustomerName, City FROM Customers;

Result: 8 rows

CustomerNameCity
Aarav TradersMumbai
Blue Harbor FoodsLondon
Cedar & CoToronto
Delta MartBengaluru
Evergreen GrocersNew York
Fjord SuppliesOslo
Golden SpoonNULL
Harbor LightsLondon

Select ALL Columns

Example

Return all columns from the Products table:

SELECT * FROM Products;

Result: 6 rows

ProductIDProductNameCategoryPriceStock
1Basmati Rice 5kgGrocery12.5120
2Green Tea 100 bagsBeverages6.75300
3Olive Oil 1LGrocery9.9980
4Dark ChocolateSnacks3.490
5Espresso Beans 1kgBeverages1845
6Almonds 500gSnacks8.25NULL

Tip: Avoid SELECT * in application code. Listing columns makes queries faster (less data sent), and they keep working when someone adds a column to the table.

Calculated Columns

Example

A SELECT can compute values. Show each product's stock value:

SELECT ProductName, Price, Stock, Price * Stock AS StockValue
FROM Products;

Result: 6 rows

ProductNamePriceStockStockValue
Basmati Rice 5kg12.51201500
Green Tea 100 bags6.753002025
Olive Oil 1L9.9980799.2
Dark Chocolate3.4900
Espresso Beans 1kg1845810
Almonds 500g8.25NULLNULL

Exercise

Exercise: Write a query that returns the FirstName, LastName and Salary of all employees.

Show answer
SELECT FirstName, LastName, Salary FROM Employees;