Introduction to SQL
Here you can access the books table used in the course.
Note: When using sample integrations such as those that contain course data, you have read-only access. You can run queries, but cannot make any changes such as adding, deleting, or modifying the data (e.g., creating tables, views, etc.).
Take Notes
Add notes about the concepts you've learned and SQL cells with queries you want to keep.
Summary
Remember that in SQL, keywords are typically capitalized, while the rest of the query is written in lowercase. The termination point of a query is indicated by a semicolon (;).
SQL DISTINCT Keyword The DISTINCT keyword is used in a SELECT statement to return only distinct (different) values. It eliminates duplicate rows from the result set.
Example:
Corrected code:
SELECT DISTINCT column_name FROM table_name;
SQL VIEW Keyword The VIEW keyword is used to create a virtual table based on the result of a SELECT statement. It allows you to store a query as a virtual table, which can be used like a regular table in subsequent queries.
Example:
Corrected code:
CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition;
To limit the number of rows returned in a query, the syntax differs between PostgreSQL and SQL Server. In PostgreSQL, you can use the LIMIT clause to restrict the number of rows returned:
Example:
Corrected code:
SELECT column_name FROM table_name LIMIT number_of_rows;
In SQL Server, you can use the TOP clause to limit the number of rows returned:
Example:
Corrected code:
SELECT TOP number_of_rows column_name FROM table_name;
Note that in SQL Server, you can also use the OFFSET-FETCH clause to skip a certain number of rows and then fetch a specific number of rows:
Example:
Corrected code:
SELECT column_name FROM table_name ORDER BY column_name OFFSET number_of_rows_to_skip ROWS FETCH NEXT number_of_rows_to_fetch ROWS ONLY;
-- Add your own queries here
SELECT *
FROM booksExplore 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.