SQL Tutorial Flashcards
SQL
Structured Query Language
RDBMS
Relational Database Management System
How is the data in RDBMS stored?
The data in RDBMS is stored in database objects called tables.
Table
A table is a collection of related data entries and it consists of columns and rows.
Fields / Columns
A field is a column in a table that is designed to maintain specific information about every record in the table.
A column is a vertical entity in a table that contains all information associated with a specific field in a table.
Record and/or Row
A record is a horizontal entity in a table.
Select
The SELECT statement is used to select data from a database.
Syntax
SELECT column1, column2, …
FROM table_name;
Select All
If you want to return all columns, without specifying every column name, you can use the SELECT * syntax:
SELECT * FROM Customers;
The SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
SELECT DISTINCT column1, column2, …
FROM table_name;
Count Distinct
By using the DISTINCT keyword in a function called COUNT, we can return the number of different countries.
SELECT Count(*) AS DistinctCountries
FROM (SELECT DISTINCT Country FROM Customers);
The SQL WHERE Clause
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
SELECT *
FROM Customers
WHERE Country=’Mexico’;
Operators in The WHERE Clause
= , > , < , >= , <= , <> , BETWEEN , LIKE , IN
=
Equal
>
Greater than
<
Less than
> =
Greater than or equal
<=
Less than or equal
<>
Not equal
!=
Not equal
BETWEEN
between a certain range
LIKE
search for a pattern
IN
To Specify multiple possible values for a column
ORDER BY
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
SELECT column1, column2, …
FROM table_name
ORDER BY column1, column2, … ASC|DESC;
ORDER BY Several Columns
SELECT * FROM Customers
ORDER BY Country, CustomerName;
Using Both ASC and DESC
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
AND
The WHERE clause can contain one or many AND operators.
The AND operator is used to filter records based on more than one condition
SELECT *
FROM Customers
WHERE Country = ‘Spain’ AND CustomerName LIKE ‘G%’;
AND vs OR
The AND operator displays a record if all the conditions are TRUE.
The OR operator displays a record if any of the conditions are TRUE.
All Conditions Must Be True
The following SQL statement selects all fields from Customers where Country is “Germany” AND City is “Berlin” AND PostalCode is higher than 12000:
SELECT * FROM Customers
WHERE Country = ‘Germany’
AND City = ‘Berlin’
AND PostalCode > 12000;
Combining AND and OR
The following SQL statement selects all customers from Spain that starts with a “G” or an “R”.
Make sure you use parenthesis to get the correct result.
Select all Spanish customers that starts with either “G” or “R”:
SELECT * FROM Customers
WHERE Country = ‘Spain’ AND (CustomerName LIKE ‘G%’ OR CustomerName LIKE ‘R%’);
Select all customers that either:
are from Spain and starts with either “G”, or
starts with the letter “R”:
SELECT * FROM Customers
WHERE Country = ‘Spain’ AND CustomerName LIKE ‘G%’ OR CustomerName LIKE ‘R%’;
OR
The WHERE clause can contain one or more OR operators.
The OR operator is used to filter records based on more than one condition, like if you want to return all customers from Germany but also those from Spain:
Select all customers from Germany or Spain:
SELECT *
FROM Customers
WHERE Country = ‘Germany’ OR Country = ‘Spain’;
OR vs AND
The OR operator displays a record if any of the conditions are TRUE.
The AND operator displays a record if all the conditions are TRUE.
Combining AND and OR
You can combine the AND and OR operators.
The following SQL statement selects all customers from Spain that starts with a “G” or an “R”.
SELECT * FROM Customers
WHERE Country = ‘Spain’ AND (CustomerName LIKE ‘G%’ OR CustomerName LIKE ‘R%’);
NOT
The NOT operator is used in combination with other operators to give the opposite result, also called the negative result.
SELECT column1, column2, …
FROM table_name
WHERE NOT condition;
SELECT * FROM Customers
WHERE NOT Country = ‘Spain’;
NOT LIKE
Select customers that does not start with the letter ‘A’:
SELECT * FROM Customers
WHERE CustomerName NOT LIKE ‘A%’;
NOT BETWEEN
Select customers with a customerID not between 10 and 60:
SELECT * FROM Customers
WHERE CustomerID NOT BETWEEN 10 AND 60;
NOT IN
Select customers that are not from Paris or London:
SELECT * FROM Customers
WHERE City NOT IN (‘Paris’, ‘London’);
NOT Greater Than
Select customers with a CustomerID not less than 50:
SELECT * FROM Customers
WHERE NOT CustomerId < 50;
Note: There is a not-less-than operator: !< that would give you the same result.
INSERT INTO
The INSERT INTO statement is used to insert new records in a table.
INSERT INTO statement in two ways
- Specify both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, …)
VALUES (value1, value2, value3, …);
- If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the INSERT INTO syntax would be as follows:
INSERT INTO table_name
VALUES (value1, value2, value3, …);
NULL Values
A field with a NULL value is a field with no value.
If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value.
Note: A NULL value is different from a zero value or a field that contains spaces. A field with a NULL value is one that has been left blank during record creation!
How to Test for NULL Values?
It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
We will have to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
IS NOT NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
IS NULL
The IS NULL operator is used to test for empty values (NULL values).
The following SQL lists all customers with a NULL value in the “Address” field:
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
Tip: Always use IS NULL to look for NULL values.
IS NOT NULL
The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).
The following SQL lists all customers with a value in the “Address” field:
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;
SELECT TOP
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.
Select only the first 3 records of the Customers table:
SELECT TOP 3 * FROM Customers;
LIMIT
Select the first 3 records of the Customers table:
SELECT * FROM Customers
LIMIT 3;
FETCH FIRST
Select the first 3 records of the Customers table:
SELECT * FROM Customers
FETCH FIRST 3 ROWS ONLY;
TOP PERCENT
SELECT TOP 50 PERCENT * FROM Customers;
SELECT * FROM Customers
FETCH FIRST 50 PERCENT ROWS ONLY;
ORDER BY
Add the ORDER BY keyword when you want to sort the result, and return the first 3 records of the sorted result.
SELECT * FROM Customers
ORDER BY CustomerName DESC
LIMIT 3;
SQL Aggregate Functions
An aggregate function is a function that performs a calculation on a set of values, and returns a single value.
Aggregate functions are often used with the GROUP BY clause of the SELECT statement. The GROUP BY clause splits the result-set into groups of values and the aggregate function can be used to return a single value for each group