Select all data from DB

SELECT * FROM <table_name>;

Select a column from table

SELECT <col name> FROM <table_name>;

Select multiple columns from table

SELECT <col name>, <col name> FROM <table_name>;

Change column name, making an alias

SELECT first_name AS" First name" FROM <table_name>;
SELECT first_name AS" First name", last_name AS Surname FROM table_name;

Retrieve specific information

SELECT <columns> FROM <table> WHERE <condition>;
SELECT first_name, last_name FROM people WHERE last_name = "Bloggs";
SELECT first_name, last_name FROM people WHERE last_name = "Bloggs" AND first_name = "Joe";
SELECT first_name, last_name FROM people WHERE last_name = "Bloggs" OR first_name = "Joe";

Compare multiple values

SELECT <columns> FROM <table> WHERE <condition> IN (value1, value 2, value 3);
SELECT * FROM people WHERE last_name IN ("Bloggs", "Shearer");

//opposite
SELECT * FROM people WHERE last_name NOT IN ("Bloggs", "Shearer");

Between a particular range

SELECT <columns> FROM <table> WHERE <columns> BETWEEN <minimum> AND <maximum>;
SELECT * FROM people WHERE age BETWEEN 1 AND 21;

Find data that matches pattern (has to contain a woldcard)

SELECT <columns> FROM <table> WHERE <columns> LIKE <whatever>

// % means any number of characters, search query is case insensitive
SELECT * FROM people WHERE name LIKE "%Shane%";

Filtering out or finding missing info

SELECT <columns> FROM <table> WHERE <columns> IS NULL;

SELECT * FROM people WHERE name = "Paul" AND age IS NULL;
SELECT * FROM people WHERE name = "Paul" AND age IS NOT NULL;

Posted on

Other library topics

Next/Prev

Worthy of Note

Worthy of Note is a site aimed at Web Designers & Developers. It offers a wide range of resources to help assist anyone in the web industry.

View