SQL SELECT Statement



Introduction

The SELECT statement is the fundamental way to retrieve data from a database table. It allows you to choose specific columns or all columns from a table and display the results.

Basic Syntax

Syntax

SELECT column1, column2, ...
FROM table_name;
            

Here:

  • column1, column2, ... represents the names of the columns you want to select. You can list as many columns as needed, separated by commas.
  • table_name is the name of the table from which you're retrieving the data.

Example: Selecting Specific Columns

Example: Selecting Customer Name and City

This example selects the CustomerName and City columns from the Customers table.

SQL Query

SELECT CustomerName, City FROM Customers;
            
Output

-- Output will vary depending on the data in your Customers table. Example:
-- CustomerName                      | City
-- ---------------------------------- | --------
-- Alfreds Futterkiste                | Berlin
-- Ana Trujillo Emparedados y helados | México D.F.
-- Antonio Moreno Taquería           | México D.F.
-- ...and so on...
            

Selecting All Columns

If you want to retrieve all columns from a table, you can use the asterisk (*) wildcard:

Syntax

SELECT * FROM table_name;
            

Example: Selecting All Columns

This example retrieves all columns from the Customers table.

SQL Query

SELECT * FROM Customers;
            
Output

-- Output will show all columns from the Customers table.  Example:
-- CustomerID | CustomerName                      | ContactName       | Address                | City       | PostalCode | Country
-- ---------- | ---------------------------------- | ------------------ | ---------------------- | ---------- | ---------- | --------
-- 1          | Alfreds Futterkiste                | Maria Anders        | Obere Str. 57          | Berlin     | 12209      | Germany
-- 2          | Ana Trujillo Emparedados y helados | Ana Trujillo        | Avda. de la Constitución 2222 | México D.F. | 05021      | Mexico
-- ...and so on...

            

Demo Database: Customers Table

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden