Your first select
Every SQL journey starts with one keyword: SELECT. It is the command that retrieves data from a table. Think of a table as a spreadsheet, and SELECT lets you pick which columns to see and from which sheet.
SELECT * is great for exploration, especially when you are first looking at a table and want to see what columns exist. In production queries, you should list specific columns because it makes your query self-documenting, avoids sending unnecessary data over the network, and will not break if someone adds a column later.
-- Select specific columns with aliases
SELECT
customer_city AS city,
customer_state AS state
FROM customers
LIMIT 10;
-- AS renames the column in your results
-- The actual table column is unchangedListing columns explicitly is clearer and more efficient. AS creates an alias, which is a temporary rename for your results.
Now that you know how to select specific columns, let us explore a few more tables in the dataset to see what data is available.
-- Explore the orders table
SELECT * FROM orders LIMIT 5;
-- Explore products with specific columns
SELECT
product_id,
product_category_name,
product_weight_g,
product_photos_qty
FROM products
LIMIT 5;Get comfortable exploring different tables. Each one holds a different piece of the e-commerce puzzle.
Fill in the blanks: Write your own select
Loading practice…
Quiz: Quiz
Loading practice…
Flashcards: Flashcards
Loading practice…