SQL SELECT Statement

The SELECT statement is one of the most fundamental SQL commands. It is used to retrieve data from one or more tables in a database.


Syntax

SELECT column1, column2, ...
FROM table_name;
  • column1, column2, ...: The names of the columns you want to retrieve data from.
  • table_name: The name of the table containing the data.

Example: Selecting Specific Columns

To retrieve specific columns from a table, specify the column names in the SELECT statement.
For instance, to get the CustomerName and City from the Customers table:

SELECT CustomerName, City 
FROM Customers;

Selecting All Columns

To fetch all columns in a table without listing their names, use * (asterisk).
Example:

SELECT * 
FROM Customers;

This will return every column and row in the table.


Demo Database: Customers Table

Consider the following Customers table as an 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
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

Examples

  1. Select Specific Columns
    To get the customer name and country:

    SELECT CustomerName, Country 
    FROM Customers;
    
  2. Select All Columns
    Fetch all information about all customers:

    SELECT * 
    FROM Customers;
    

Key Points

  • Column Order Matters: The result will display columns in the order specified in the SELECT statement.
  • Case Insensitivity: SQL keywords are not case-sensitive, so SELECT and select are the same.
  • Simplifying Queries: Using * is convenient but may include unnecessary data. For better performance and clarity, specify column names when possible.

The SELECT statement is the cornerstone of SQL queries, offering flexible ways to retrieve data tailored to your needs. Whether selecting specific columns or entire tables, it provides a powerful tool for interacting with your database.


Was this article helpful?