Database Tables
Database contains 1 or more tables
Keywords are NOT case sensitive: select == SELECT
Important SQL Commands
SELECT: used to select data from a database
Syntax
SELECT column1, column2, ...
FROM table_name;
SELECT DISTINCT column1, column2, ...
FROM table_name;
Number of different customer countries:
SELECT COUNT(DISTINCT Country) FROM Customers;
: to filter records → extract only records that fulfill a specified condition
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
SELECT * FROM Customers
WHERE Country = 'Mexico';
Text Fields vs. Numeric Fields
SQL requires single quotes around text values
Numeric fields should NOT be enclosed in quotes
SELECT * FROM Customers
WHERE CustomerID = 1;
Operators in WHERE clause
= Equal, > Greater than, < Less than, >= Greater than or equal, <= Less than or equal, <> (!=) Not equal,
BETWEEN Between a certain range, LIKE Search for a pattern, IN To search multiple possible values for a column
WHERE clause can be combined w/ AND, OR, and NOT operators
AND:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT:
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
This post is based on W3School's articles about SQL.