fbpx

SQL Server – SELECT Statement

This SQL tutorial explores the basic concepts of SQL in SQL Server and demonstrates how to create a simple SELECT statement. For this lesson’s exercises, use this link.

This tutorial is a part of several posts explaining how to write basic queries in SQL Server. To read additional posts regarding this subject, please use the following links:


Basic Select Statement

the syntax for a basic SELECT statement in SQL Server is:

SELECT *
FROM table_name

For example :

SELECT *
FROM employees

In this example we displayed all columns of data in Employees table.

The SQL Server SELECT clause:

  • Let you choose what you want to display.
  • The asterisk sign (*) indicates that you want to select all fields contained in this table.

The SQL Server FROM clause:

  • Let you specify from which table you want to retrieve all of these fields.
  • A table’s name always appears after the FROM keyword.

Selecting Specific Columns

the syntax for selecting specific columns in SQL Server is:

SELECT column_name, column_name, column_name ..
FROM table_name

For example :

SELECT last_name , first_name
FROM employees

The example above retrieves specific columns.

The SQL Server SELECT clause:

  • Let you choose what columns you want to display (“Projection”).
  • After the SQL Server SELECT keyword, specify the names of the columns that you would like to retrieve, separated by comma (,).
  • You can specify as many columns as you want; you can even specify the same column more than once.
  • The columns appear in the order selected.

The SQL Server FROM clause:

  • Let you specify from which table you want to display these columns.
  • A table’s name always appears after the SQL Server FROM keyword.

General Guidelines

  • In each SQL statement, SQL Server SELECT and FROM clauses are mandatory. Without both of them, SQL statements are not valid (there is no point in seeking to display data without indicating what should be retrieved and from where).
-- SQL Statement 1 - not valid (FROM clause is missing)
SELECT last_name, first_name, salary

-- SQL Statement 2 - not valid (SELECT clause is missing)
FROM employees 

-- SQL Statement 3 - valid
SELECT last_name, first_name, salary
FROM employees
  • The order of the SQL Server SELECT and FROM clauses cannot be changed, the SQL Server SELECT clause will always be listed first; you cannot write a SQL Server SELECT statement that begins with a FROM clause.
-- Not a valid SQL statement
FROM employees
SELECT last_name, first_name, salary
  • It is possible to specify a column’s name multiple times; the data of this column will simply appear again and again, according to the number of times the column was specified.
SELECT employee_id, last_name, last_name, last_name
FROM employees
  • To enhance readability – even though the SQL Server SQL syntax is neither case-sensitive, nor sensitive to spaces or line breaks, ensure writing in an orderly manner: write the keywords in capital letters, names of columns/tables in small letters, insert a line break after each command and indents when required.

UpScale Analytics is one of the largest platforms in the world for learning SQL by doing, consisting over 300 SQL exercises at different levels (including solutions), by topics, across over 100 different datasets. More…