- download and install SQLite
- download
northwind.db(included here in this repo) - open the database using a command like
sqlite3 ~/Downloads/northwind.db(this will put you in a shell that is connected to the database file) - type
.tablesto see the available tables - run the query
select * from Shippers; - type
.mode column - run the query again (note the difference)
- do the questions and answers
- Get all columns and rows from the
Regionstable - How many rows are in the
Orderstable? - From the
Categoriestable, get theCategoryNameandDescriptionof all rows, sorted byCategoryNamein ascending alphabetical order - From the
Customerstable, get theCompanyNameandContactNameof everyone in theCityofBuenos Aires - How many
Customershave aCountrythat isn'tGermany,Spain, orMexico? ACountryofnullis presumed to not be one of those countries. - From the
Customerstable, return all the countries present (Country) in alphabetical order (with no duplicates) - From the
Customerstable, list all cities (City) starting with the letterAorC. Don't include duplicates. - In the
Orderstable, oneCustomerIDmay be associated with manyOrderIDs. Get the count of orders perCustomerID, in ascending alphabetical order ofCustomerID. What's the first result? - There are 77 products in the
Productstable, and 29 suppliers in theSupplierstable. Using aleft join, list the name of all products (and the name of their supplier) where theCompanyNameof the supplier is one ofExotic Liquids,Specialty Biscuits, Ltd.,Escargots Nouveaux.
-
select * from Regions;- this is the most basic SQL query which you will use all the time,
*means 'all columns', and since we're not restricting the rows in any way, we get all the rows
- this is the most basic SQL query which you will use all the time,
-
select count(*) from Orders;- answer:
830
- answer:
-
select CategoryName, Description from Categories order by CategoryName asc;You can do on one line, but the formatting below is better:
select CategoryName, Description from Categories order by CategoryName asc;
-
select CompanyName, ContactName from Customers where City = 'Buenos Aires';- double quotes will work in SQLite, ie
"Buenos Aires", but use single quotes to get into the habit for PostgreSQL
- double quotes will work in SQLite, ie
-
select count(*) from Customers where Country not in ('Germany', 'Mexico', 'Spain') or Country is null;- answer:
72 - half marks if you didn't include
or Country is null. When you think ofnull, think "unknown". Is an unknown country not in Germany, Mexico, Spain? SQL doesn't know, so will conservatively just give you70. We have to explicitly deal with thenullcase.
- answer:
-
select distinct Country from Customers order by Country asc;- note,
distinctworks by sorting the results behind the scenes, so be conscious of that when working with large data (because it will have to sort the entire table every single time)
- note,
-
select distinct City from Customers where City like 'A%' or City like 'C%';- sqlite unfortunately doesn't have
ilikewhich is case-insensitive (not that it matters in this case) like '%blah%is a very common pattern to see if some particular text is present anywhere in a string
- sqlite unfortunately doesn't have
-
select CustomerID, count(OrderID) from Orders group by CustomerID order by CustomerID asc;- answer:
ALFKI 6
- answer:
-
🐧
- Note, normally a table's own ID is just
id, soleft join Suppliers on Suppliers.ID = Products.SupplierIDwould be more typical, but this database isn't named like that. - Also, I like to be very explicit every time with which table a column came from, so
select Products.ProductName, Suppliers.CompanyName ...instead ofselect ProductName, CompanyName ..., even though the 2nd one will work too in this case because those no ambiguity about which table they came from.
select Products.ProductName, Suppliers.CompanyName from Products left join Suppliers on Suppliers.SupplierID = Products.SupplierID where Suppliers.CompanyName in ('Exotic Liquids', 'Specialty Biscuits, Ltd.', 'Escargots Nouveaux');
Output, order doesn't matter
ProductName CompanyName -------------------------- ------------------------ Chai Exotic Liquids Chang Exotic Liquids Aniseed Syrup Exotic Liquids Teatime Chocolate Biscuits Specialty Biscuits, Ltd. Sir Rodney's Marmalade Specialty Biscuits, Ltd. Sir Rodney's Scones Specialty Biscuits, Ltd. Escargots de Bourgogne Escargots Nouveaux Scottish Longbreads Specialty Biscuits, Ltd. - Note, normally a table's own ID is just