SQL Flashcards
Write a simple select query with a condition and sorting, no join
SELECT * FROM MyTable WHERE MyColumn > 50 ORDER BY MyColumn
Write a query with inner join
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM Orders INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
Create a table
CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (PersonID) );
Insert row(s) to a table
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'); -- If using all the columns you can eliminate the column names INSERT INTO Customers VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'); -- insert into with select INSERT INTO Customers (CustomerName, City, Country) SELECT SupplierName, City, Country FROM Suppliers;
Update data in a table row
UPDATE Customers SET ContactName = 'Alfred Schmidt', City= 'Frankfurt' WHERE CustomerID = 1;
Discuss Join types and show how to use each of them
(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
Do a group by query with some aggregate
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
List SQL data types
Show how to use a foreign key and discuss why it’s used
Talk about indexes, give an example using one
Use some common constraints and show how they are used.