SQL Joins Flashcards
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.
Write an IN statement.
WHERE Country IN (‘Germany’, ‘France’, ‘UK’);
What is the Between operator?
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
Write a statement using Between.
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
What is the Exists command used for?
The EXISTS operator is used to test for the existence of any record in a subquery.
Write an Exists statement.
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price = 22);
How is Select Into used?
The SELECT INTO statement copies data from one table into a new table.
Write a Select Into statement..
SELECT * INTO CustomersBackup2017
FROM Customers;
What is a Stored Procedure?
A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again.
So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it.
You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter value(s) that is passed.