SELECT statement

SELECT *
FROM movies;

Clauses

WHERE clause

Fitler rows to match a condition.

SELECT title
FROM library
WHERE pub_year = 2017;

AS clause

Rename colums or tables in the result set.

SELECT name AS 'movie_title'
FROM movies;

ORDER BY clause

Orders the result based on a column and order type.

SELECT *
FROM contacts
ORDER BY birth_date DESC;

DISTINCT clause

Removes duplicate rows from the result set

SELECT DISTINCT city
FROM contact_details;

LIMIT clause

Limits the result set up to a specified number of rows.

SELECT *
FROM movies
LIMIT 5;

Operators

AND operator

Combines multiple condition with AND.

SELECT model 
FROM cars 
WHERE color = 'blue' 
  AND year > 2014;

OR operator

Combines multiple conditions with OR.

SELECT name
FROM customers
WHERE state = 'CA'
   OR state = 'NY';

NOT operator

Negates a condition.

SELECT address
FROM records
WHERE address IS NOT NULL;

IN operator

Check if a value is in a collection.

SELECT name, degree
FROM students
WHERE degree IN ("Math", "Statistics", "Computer Science");

BETWEEN operator

Filter values based on a range.

SELECT *
FROM movies
WHERE year BETWEEN 1980 AND 1990;

LIKE operator

Conditional with pattern matching.

SELECT name
FROM movies
WHERE name LIKE 'Star%';

NOTE

  • % wildcard can be used along with LIKE to match 0 unspecified characters.
  • wildcard can be used to match 1 unspecified character.