Usage
Select data from the database, filter it as need be
The statementUSE database tells the server which database to use
USE northwind
SELECT employee_id, lastname, firstname, title
FROM employees
Filter it with WHERE
WHERE
SELECT employee_id, lastname, firstname, title
FROM employees
WHERE lastname = 'Smith'
Search for multiple values with WHERE col IN
IN
SELECT employee_id, lastname, firstname, title
FROM employees
WHERE lastname IN ('Smith', 'Reynolds')
Find a string with the wildcard and LIKE
command
LIKE
SELECT employee_id, lastname, firstname, title
FROM employees
WHERE lastname LIKE '%mith'
Choose a range with BETWEEN
BETWEEN
SELECT employee_id, lastname, firstname, title
FROM employees
WHERE employee_id BETWEEN 1 AND 30
NULL
is a non value
NULL
SELECT employee_id, lastname, firstname, title
FROM employees
WHERE title IS NULL
NOT
is a negative value
NOT
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
ORDER BY
value ASC/DSC
SELECT employee_id, lastname, firstname, title
FROM employees
ORDER BY employee_id ASC
Change the column name with AS
to make an alias
AS
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
CONCAT
SELECT CONCAT('Employee ID:', employee_id) AS [Employee ID], lastname, firstname, title
FROM employees
Choose only unique values for a given column with DISTINCT
DISTINCT
SELECT DISTINCT title
FROM employees