SQL Joins Flashcards
(82 cards)
What is a SQL join?

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
What is an Inner Join?
Selects records that have matching values in both tables.
Write and Inner Join.

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM Orders INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
What is a Left Join?

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.
Write a Left Join.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
What is a Right Join?

The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records from the left table (table1). The result is 0 records from the left side, if there is no match.
Write a Right Join.
The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records from the left table (table1). The result is 0 records from the left side, if there is no match.
What is a Full Join?

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Write a Full Join.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
What is a Self Join?
A self join is a regular join, but the table is joined with itself.
Write a Self Join.
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
What is a Union?
The UNION operator is used to combine the result-set of two or more SELECT statements.
Write a Union.
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
What is GroupBy?
The GROUP BY statement groups rows that have the same values into summary rows, like “find the number of customers in each country”.
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
The GROUP BY statement groups rows that have the same values into summary rows, like “find the number of customers in each country”.
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
Write a GroupBy.
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
What is the Having Clause used for?
The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.
Write a Having Statement.
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
What is the OderBy Clause used for?
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
Write an OrderBy statement.
SELECT column1, column2, …
FROM table_name
ORDER BY column1, column2, … ASC|DESC;
How do you How do you modify existing records in a table?
The UPDATE statement is used to modify the existing records in a table.
Write and update statement.
UPDATE table_name
SET column1 = value1, column2 = value2, …
WHERE condition;
How do you put records in a table?
The INSERT INTO statement is used to insert new records in a table.
Write an Insert statement.
Write an Insert statement.
What is the IN operator?
The IN operator allows you to specify multiple values in a WHERE clause.
















