Introduction to SQL
Here you can access the books table used in the course.
Take Notes
Add notes about the concepts you've learned and SQL cells with queries you want to keep.
Add your notes here
Querying the books table You're ready to practice writing your first SQL queries using the SELECT and FROM keywords. Recall from the video that SELECT is used to choose the fields that will be included in the result set, while FROM is used to pick the table in which the fields are listed.
Feel free to explore books in the exercise. Let's zoom in on this table in the database schema to see the fields and data types it contains.
Your task in this exercise is to practice selecting fields from books.
-- Add your own queries here
SELECT *
FROM books-- Select title and author from the books table
SELECT title, author
FROM books;-- Select all fields from the books table
SELECT *
FROM books;Making queries DISTINCT You've learned that the DISTINCT keyword can be used to return unique values in a field. In this exercise, you'll use this understanding to find out more about the books table!
There are 350 books in the books table, representing all of the books that our local library has available for checkout. But how many different authors are represented in these 350 books? The answer is surely less than 350. For example, J.K. Rowling wrote all seven Harry Potter books, so if our library has all Harry Potter books, seven books will be written by J.K Rowling. There are likely many more repeat authors!
-- Select unique authors from the books table
SELECT DISTINCT author FROM books-- Select unique authors and genre combinations from the books table
SELECT DISTINCT author, genre
FROM books;-- Alias author so that it becomes unique_author
SELECT DISTINCT author as unique_author
FROM books;-- Save the results of this query as a view called library_authors
create view library_authors AS
SELECT DISTINCT author AS unique_author
FROM books;-- Select the first 10 genres from books using PostgreSQL
SELECT genre FROM books
LIMIT 10Explore Datasets
Use the books table to explore the data and practice your skills!
- Select only the
titlecolumn. - Alias the
titlecolumn asbook_title. - Select the distinct author names from the
authorcolumn. - Select all records from the table and limit your results to 10.