DEV Community

Cover image for Basics SQL - SELECT Information
Anne Quinkenstein
Anne Quinkenstein

Posted on • Edited on

Basics SQL - SELECT Information

Cheat-Sheet

Statements

SELECT

SELECT column1, column2, ...
FROM table_name; 
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM table_name; 
Enter fullscreen mode Exit fullscreen mode

Filter with WHERE

SELECT firstname, birthday FROM customers WHERE lastname='Mueller';
Enter fullscreen mode Exit fullscreen mode
  • an equal value = or different value !=
  • a higher value > (or higher than or equal to >=), a lower value < (or lower than or equal to <=)
  • a value from several possible values with IN
  • a numerical value (or a date) from within a range BETWEEN xx AND yy
  • a chain starting with or ending with with LIKE and the % joker
  • a value IS NULL or IS NOT NULL

Limit the result

SELECT <champs> FROM <table> LIMIT <nb_results>;
SELECT * FROM customer LIMIT 5 OFFSET 20;
Be careful: it is a common mistake to write LIMIT 25 OFFSET 20, assuming this will retrieve results 21 to 25, but it would actually return results 21 to 45!)

ORDER BY Sorting and filtering

SELECT firstname, lastname FROM customer ORDER BY lastname ASC, birthday DESC;
the youngest Memebers of Müller:

SELECT * FROM customer WHERE lastname='Müller' ORDER BY birthday DESC LIMIT 0,3;
Enter fullscreen mode Exit fullscreen mode

A Challenge - try yourself

Top comments (0)