SELECT Statement


Usage

Select data from the database, filter it as need be

The statement USE database tells the server which database to use

USE northwind
SELECT employee_id, lastname, firstname, title
FROM employees

Filter it with

WHERE


SELECT employee_id, lastname, firstname, title
FROM employees
WHERE lastname = 'Smith'

Search for multiple values with WHERE col

IN


SELECT employee_id, lastname, firstname, title
FROM employees
WHERE lastname IN ('Smith', 'Reynolds')

Find a string with the wildcard and

LIKE

command


SELECT employee_id, lastname, firstname, title
FROM employees
WHERE lastname LIKE '%mith'

Choose a range with

BETWEEN


SELECT employee_id, lastname, firstname, title
FROM employees
WHERE employee_id BETWEEN 1 AND 30

NULL

is a non value


SELECT employee_id, lastname, firstname, title
FROM employees
WHERE title IS NULL

NOT

is a negative value


SELECT employee_id, lastname, firstname, title
FROM employees
WHERE title IS NOT NULL

ORDER BY

value ASC/DSC
will display the results in a particular order


SELECT employee_id, lastname, firstname, title
FROM employees
ORDER BY employee_id ASC

Change the column name with

AS

to make an alias


SELECT employee_id, lastname AS Last, firstname AS First, title
FROM employees

Concatenate a string literal with the

CONCAT

function

Square brackets encapsulate a string

SELECT CONCAT('Employee ID:', employee_id) AS [Employee ID], lastname, firstname, title
FROM employees

Choose only unique values for a given column with

DISTINCT


SELECT DISTINCT title
FROM employees