Careeroza
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
| CustomerName | City |
|---|---|
| Aarav Traders | Mumbai |
| Blue Harbor Foods | London |
| Cedar & Co | Toronto |
| Delta Mart | Bengaluru |
| Evergreen Grocers | New York |
| Fjord Supplies | Oslo |
| Golden Spoon | NULL |
| Harbor Lights | London |
Select ALL Columns
Example
Return all columns from the Products table:
SELECT * FROM Products;Result: 6 rows
| ProductID | ProductName | Category | Price | Stock |
|---|---|---|---|---|
| 1 | Basmati Rice 5kg | Grocery | 12.5 | 120 |
| 2 | Green Tea 100 bags | Beverages | 6.75 | 300 |
| 3 | Olive Oil 1L | Grocery | 9.99 | 80 |
| 4 | Dark Chocolate | Snacks | 3.49 | 0 |
| 5 | Espresso Beans 1kg | Beverages | 18 | 45 |
| 6 | Almonds 500g | Snacks | 8.25 | NULL |
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
| ProductName | Price | Stock | StockValue |
|---|---|---|---|
| Basmati Rice 5kg | 12.5 | 120 | 1500 |
| Green Tea 100 bags | 6.75 | 300 | 2025 |
| Olive Oil 1L | 9.99 | 80 | 799.2 |
| Dark Chocolate | 3.49 | 0 | 0 |
| Espresso Beans 1kg | 18 | 45 | 810 |
| Almonds 500g | 8.25 | NULL | NULL |
Exercise
Exercise: Write a query that returns the FirstName, LastName and Salary of all employees.
Show answer
SELECT FirstName, LastName, Salary FROM Employees;