SQL Terms Flashcards
SELECT DISTINCT
Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
SELECT DISTINCT Country FROM Customers; SELECT COUNT(DISTINCT Country) FROM Customers;
Example of SQL AND, OR and NOT Operators
SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND condition3 ...; SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ...; SELECT column1, column2, ... FROM table_name WHERE NOT condition;
The SQL ORDER BY Keyword
SELECT * FROM Customers ORDER BY Country DESC;
The SQL SELECT LIMIT Clause
SELECT * FROM Customers WHERE Country='Germany' LIMIT 3;
The SQL MIN() and MAX() Functions
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
SELECT MAX(Price) AS LargestPrice FROM Products;
The SQL COUNT(), AVG() and SUM() Functions
The COUNT() function returns the number of rows that matches a specified criterion.
The AVG() function returns the average value of a numeric column.
The SUM() function returns the total sum of a numeric column.
SELECT COUNT(ProductID) FROM Products; SELECT AVG(Price) FROM Products;
The SQL LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
E.g The following SQL statement selects all customers with a CustomerName starting with “a”:
SELECT * FROM Customers WHERE CustomerName LIKE 'a%';
The SQL IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.
~~~
SELECT * FROM Customers
WHERE Country IN (‘Germany’, ‘France’, ‘UK’);
SELECT * FROM Customers
WHERE Country NOT IN (‘Germany’, ‘France’, ‘UK’);
SELECT * FROM Customers
WHERE Country IN (SELECT Country FROM Suppliers);
~~~
The SQL BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
SELECT * FROM Products WHERE Price BETWEEN 10 AND 20 AND CategoryID NOT IN (1,2,3); SELECT * FROM Products WHERE ProductName NOT BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni' ORDER BY ProductName;
SQL Aliases
SQL aliases are used to give a table, or a column in a table, a temporary name.
An alias is created with the AS keyword.
SELECT CustomerID AS ID, CustomerName AS Customer FROM Customers;
Different Types of SQL JOINs
(INNER) JOIN
: Returns records that have matching values in both tables
LEFT (OUTER) JOIN
: Returns all records from the left table, and the matched records from the right table
RIGHT (OUTER) JOIN
: Returns all records from the right table, and the matched records from the left table
FULL (OUTER) JOIN
: Returns all records when there is a match in either left or right table
SELF JOIN
: A self join is a regular join, but the table is joined with itself.
SQL INNER JOIN Keyword
The INNER JOIN keyword selects records that have matching values in both tables.
SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
SQL SELF JOIN Keyword
The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2). The result is 0 records from the right side, if there is no match.
SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City FROM Customers A, Customers B WHERE A.CustomerID <> B.CustomerID AND A.City = B.City ORDER BY A.City;
SQL UNION Operator
The UNION operator is used to combine the result-set of two or more SELECT statements.
Every SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in every SELECT statement must also be in the same order
E.g The following SQL statement returns the cities (only distinct values) from both the “Customers” and the “Suppliers” table:
SELECT City FROM Customers UNION SELECT City FROM Suppliers ORDER BY City;
What are the different subsets of SQL?
Data Definition Language (DDL) – It allows you to perform various operations on the database such as CREATE, ALTER, and DELETE objects.
Data Manipulation Language(DML) – It allows you to access and manipulate data.
Data Control Language(DCL) – It allows you to control access to the database. E.g – Grant, Revoke access permissions.