SQL Syntax Flashcards
The different parts of a SQL statement
A clause
Basic 2 elements to get data from a database
SELECT column1, column2,
FROM table_name;
Select all the DIFFERENT values from the Country column in the Customers table
SELECT DISTINCT Country
FROM Customers;
The number of different (distinct) customer countries (different countries in the customer table)
SELECT COUNT(DISTINCT Country) FROM Customers;
Conditions
All records where City Column has value “Berlin”
SELECT * FROM Customers
WHERE City = ‘Berlin’;
Select all records where City is NOT “Berlin”
SELECT * FROM Customers
WHERE NOT City = ‘Berlin’;
Sort result set in ascending or descending order
SELECT column1, column2
FROM table_name
ORDER BY column1, column2 DESC;
Ascending order by default
Test for NULL values
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
or IS NOT NULL;
Specify a number of records to return
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
This varies greatly across DBMS, including LIMIT, FETCH FIRST, and ROWNUM
What are the most popular DBMS?
MySQL, Oracle, Microsoft SQL Server, PostgreSQL, SQLite, MongoDB
Smallest or largest value in a selected column
SELECT MIN(column_name)
FROM table_name
WHERE condition;
SELECT MAX(column_name)
FROM table_name
WHERE condition;
What are some functions and where would you find them?
AVG(), SUM(), COUNT(), MIN(), MAX() Put them in the select clause SELECT SUM(Quantity)
Used in the WHERE clause to search for a specified pattern in a column
Give an example
Like
WHERE CustomerName LIKE ‘a%’
Will find any values that start with “a”
Multiple values in a WHERE clause
IN
SELECT * FROM Customers
WHERE Country IN (‘Germany’, ‘France’, ‘UK’);
Also works with NOT IN
Selects values within a given range
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;