For today we will be practicing inserting and querying data using SQL.
Here is a website that will let us write queries to interact with some data. http://jxs.me/chinook-web/
On the left are the Tables with their fields. The right is where we will be writing our queries. The bottom is where we will see our results.
Let's start with grabbing some data from the Artist table.
SELECT *
FROM Artist
This will select all the records and fields from the Artist table.
If we don't want to pull all the fields, we can be more specific in our SELECT statement
SELECT FirstName, LastName, Country
FROM Employee
If we want to put criteria on which records we pull, we use the WHERE statement.
SELECT Name, Composer, Milliseconds
FROM Track
WHERE Milliseconds > 299000
Now instead of displaying all the tracks that are over 299 seconds, we could us the count function to return only how many there are.
SELECT count(*)
FROM Track
WHERE Milliseconds > 299000
Now that we have some basic query examples. Let's try doing some more complicated ones. Use www.sqlteaching.com or sqlbolt.com as resources for the missing keywords you'll need.
- Find the average length of all tracks in Milliseconds
- Find the number of invoices in the USA
- Make a list of all the First Names of Customers that contain an 'a'
- Make a list of the 10 longest tracks
- Make a list of the 20 shortest tracks
- Find all the customers that live in California or Washington
- Find all the customers that live in California, Washington, Utah, Florida, or Arizona (Use IN keyword)
- Insert an artist to the database
- Insert yourself as a customer to the database
- Find a list of all Playlists that start with
Classical
- You can either continue exploring this dataset or look into setting up postgres on your local machine.
© DevMountain LLC, 2016. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.