CodingNic

SQL Basics

Exercises

SQL Basics 15 min read

Exercises

Objectives

This lesson introduces no new concepts. It’s a chance to practice everything from this module: SQL syntax, keywords, identifiers, comments, literals, expressions, SELECT, DISTINCT, and aliases.

All exercises are runnable against the products table set up earlier in this module.

Exercises

  1. Write a query that selects every column from products.

  2. Write a query that selects only product_name and in_stock.

  3. Write a query that returns every distinct category in products.

  4. Write a query that selects product_name and price, aliased to name and unit_price.

  5. Write a query with an expression that calculates each product’s price with 8% sales tax added (price * 1.08), aliased to price_with_tax.

  6. Add a single-line comment above your query from exercise 5 explaining what it calculates.

  7. Write a query selecting the literals 'sample', 100, and false, each with your own alias.

  8. Explain, in your own words, why SELECT DISTINCT category FROM products; returns 3 rows while SELECT category FROM products; returns 10.

Recap

You can now read and write a basic SQL statement, tell keywords from identifiers, use comments and literals correctly, write a simple expression, and control exactly what a query returns and what it’s labeled. That’s the foundation every later module’s SQL builds on.

Next module: Creating Tables, where you’ll learn to build the kind of table this module only gave you, starting with CREATE TABLE itself.