StackMonsters

PostgreSQL Course · 06

SQL Fundamentals

Query a real PostgreSQL database that runs in your browser. Filter, sort and summarize rows, join tables, write subqueries, translate relational algebra, design and normalize a schema, then add views, procedures, indexes, transactions and access control. Every table is on screen while you work, and every lab checks your query the moment you run it. Nothing to install.

16
chapters
232
lessons & labs
Chapter 1
free, no account needed
Nothing to install
PostgreSQL runs in your browser

Who it's for

Students in a database course, and engineers who query every day.

  • Computer science students taking a database course (relational model, SQL, ER design, normalization)
  • Developers and SREs who use a database every day but learned SQL by copying queries
  • Anyone preparing for a technical interview with SQL questions

What you'll be able to do

Query, design and run a relational database with confidence.

  • Write SELECT queries that filter, sort, group and summarize data
  • Combine tables with inner, outer and self joins, subqueries and set operations
  • Translate relational algebra expressions to SQL and back
  • Design a schema from an ER model and normalize it to 3NF and BCNF
  • Create tables with keys and constraints, and change data safely
  • Use views, functions, procedures, indexes, transactions and roles

The path

16 chapters covering queries, joins, schema design, performance and access control.

Each lesson is short and ends in a quiz, then a lab on a real PostgreSQL database in your browser, with every table on screen. Chapter 1 is free.

  1. 01

    First Queries

    Read a table with SELECT

    Free
    1. 1Ask the DatabaseLab3 min
    2. 2Tables, Rows and Columns4 min
    3. 3Read the TablesLab4 min
    4. 4Choose the Columns4 min
    5. 5Choose the ColumnsLab4 min
    6. 6SQL and PostgreSQL4 min
    7. 7Talk to PostgreSQLLab4 min
    8. 8Read the Error, Fix the QueryLab5 min
  2. 02

    Filter Rows

    Keep only the rows you need with WHERE

    1. 1WHERE4 min
    2. 2Keep the Rows You NeedLab5 min
    3. 3Comparison Operators3 min
    4. 4Compare ValuesLab5 min
    5. 5AND, OR and NOT4 min
    6. 6Combine ConditionsLab6 min
    7. 7IN and BETWEEN3 min
    8. 8Lists and RangesLab5 min
    9. 9LIKE Patterns4 min
    10. 10Match a PatternLab5 min
    11. 11NULL4 min
    12. 12Find the Missing ValuesLab5 min
    13. 13Challenge: Filter the ShopLab8 min
  3. 03

    Sort and Shape Results

    Order, limit and compute the columns you return

    1. 1ORDER BY4 min
    2. 2Sort the RowsLab5 min
    3. 3LIMIT and OFFSET3 min
    4. 4Top Rows and PagesLab5 min
    5. 5DISTINCT3 min
    6. 6Remove DuplicatesLab4 min
    7. 7Expressions and Aliases4 min
    8. 8Calculate and Name ColumnsLab5 min
    9. 9Functions4 min
    10. 10Use FunctionsLab6 min
    11. 11CASE3 min
    12. 12Label the RowsLab5 min
    13. 13Challenge: Shape the ReportsLab8 min
  4. 04

    Create Tables

    Define tables, add rows and change a schema

    1. 1CREATE TABLE4 min
    2. 2Create Your First TableLab5 min
    3. 3Data Types4 min
    4. 4Choose the TypesLab6 min
    5. 5INSERT a Row4 min
    6. 6Fill a TableLab6 min
    7. 7NOT NULL and DEFAULT4 min
    8. 8Required Values and DefaultsLab6 min
    9. 9Identity Columns4 min
    10. 10Let the Database Number RowsLab6 min
    11. 11ALTER TABLE4 min
    12. 12Change a Table That Holds DataLab6 min
    13. 13DROP TABLE3 min
    14. 14Clean Up the SchemaLab5 min
    15. 15Schema Migrations4 min
    16. 16Write an Up and a DownLab7 min
  5. 05

    Keys and Constraints

    Let the database refuse bad data

    1. 1Primary Keys4 min
    2. 2One Row, One KeyLab5 min
    3. 3Superkeys and Candidate Keys4 min
    4. 4Find the KeysLab6 min
    5. 5UNIQUE4 min
    6. 6No Two the SameLab6 min
    7. 7CHECK4 min
    8. 8Only Valid ValuesLab6 min
    9. 9Foreign Keys4 min
    10. 10Links That HoldLab6 min
    11. 11ON DELETE4 min
    12. 12Choose What a Delete DoesLab7 min
    13. 13Composite Keys4 min
    14. 14A Key of Two ColumnsLab6 min
  6. 06

    Change Data

    Insert, update and delete rows without surprises

    1. 1UPDATE3 min
    2. 2Change Rows with UPDATELab5 min
    3. 3Update Safely3 min
    4. 4The Missing WHERELab5 min
    5. 5DELETE3 min
    6. 6Remove Rows with DELETELab5 min
    7. 7RETURNING3 min
    8. 8Get the Changed Rows BackLab5 min
    9. 9INSERT … SELECT3 min
    10. 10Copy Rows with INSERT … SELECTLab5 min
    11. 11Upsert4 min
    12. 12Insert or UpdateLab5 min
  7. 07

    Summarize Data

    Count, total and average rows in groups

    1. 1COUNT3 min
    2. 2Count Rows and ValuesLab4 min
    3. 3SUM and AVG3 min
    4. 4Totals and AveragesLab5 min
    5. 5MIN and MAX3 min
    6. 6Smallest and LargestLab4 min
    7. 7GROUP BY4 min
    8. 8One Row per GroupLab5 min
    9. 9GROUP BY Several Columns3 min
    10. 10Groups of CombinationsLab4 min
    11. 11HAVING3 min
    12. 12Keep Some GroupsLab5 min
    13. 13WHERE vs HAVING4 min
    14. 14Rows First, Then GroupsLab5 min
  8. 08

    Join Tables

    Combine rows from related tables

    1. 1How Tables Relate3 min
    2. 2Follow the LinksLab4 min
    3. 3INNER JOIN4 min
    4. 4Join Two TablesLab5 min
    5. 5Table Aliases3 min
    6. 6Use Table AliasesLab4 min
    7. 7LEFT JOIN4 min
    8. 8Keep Every RowLab5 min
    9. 9RIGHT and FULL JOIN3 min
    10. 10Right and Full JoinsLab4 min
    11. 11Self Join3 min
    12. 12Employees and ManagersLab4 min
    13. 13Join Many Tables4 min
    14. 14Follow the ChainLab6 min
    15. 15CROSS JOIN3 min
    16. 16Every PairLab4 min
    17. 17Joins and Groups4 min
    18. 18Totals Across TablesLab6 min
    19. 19NATURAL JOIN and USING4 min
    20. 20Shared Column NamesLab5 min
  9. 09

    Subqueries and Set Operations

    Queries inside queries, and UNION, INTERSECT, EXCEPT

    1. 1Scalar Subqueries4 min
    2. 2One Value Inside a QueryLab5 min
    3. 3IN (subquery)3 min
    4. 4Membership TestsLab5 min
    5. 5NOT IN and NULL4 min
    6. 6The NULL TrapLab5 min
    7. 7EXISTS4 min
    8. 8Is There a Row?Lab5 min
    9. 9ALL, ANY and SOME3 min
    10. 10Every or At Least OneLab4 min
    11. 11Subqueries in FROM4 min
    12. 12Query a QueryLab5 min
    13. 13WITH4 min
    14. 14Name the StepsLab5 min
    15. 15UNION4 min
    16. 16Combine ResultsLab5 min
    17. 17INTERSECT and EXCEPT4 min
    18. 18Both or Only OneLab5 min
  10. 10

    Relational Algebra

    The operators under every SQL query

    1. 1Relational Algebra4 min
    2. 2Read the ExpressionsLab3 min
    3. 3Selection σ3 min
    4. 4Translate SelectionsLab4 min
    5. 5Projection π3 min
    6. 6Translate ProjectionsLab4 min
    7. 7Rename ρ3 min
    8. 8Translate RenamesLab4 min
    9. 9Cartesian Product ×4 min
    10. 10Translate ProductsLab5 min
    11. 11Joins ⋈4 min
    12. 12Translate JoinsLab5 min
    13. 13Set Operations ∪ ∩ −3 min
    14. 14Translate Set OperationsLab6 min
    15. 15Division ÷4 min
    16. 16Translate DivisionLab6 min
    17. 17Grouping γ3 min
    18. 18Translate GroupingLab4 min
    19. 19Translate Expressions4 min
    20. 20Translate ExpressionsLab6 min
  11. 11

    Design a Database

    ER models, relationships and normal forms

    1. 1Entities and Attributes4 min
    2. 2Find the AttributesLab4 min
    3. 3Relationships and Cardinality4 min
    4. 4Read Cardinality from DataLab5 min
    5. 5ER to Tables4 min
    6. 6Build Tables from an ER ModelLab6 min
    7. 7Weak Entities3 min
    8. 8Store a Weak EntityLab5 min
    9. 9Anomalies3 min
    10. 10Cause the AnomaliesLab5 min
    11. 11Functional Dependencies5 min
    12. 12Test Functional DependenciesLab6 min
    13. 13First Normal Form3 min
    14. 14Remove a Repeating GroupLab6 min
    15. 15Second Normal Form4 min
    16. 16Remove a Partial DependencyLab6 min
    17. 17Third Normal Form4 min
    18. 18Remove a Transitive DependencyLab6 min
    19. 19Boyce-Codd Normal Form5 min
    20. 20Decompose into BCNFLab7 min
    21. 21Normalize a TableLab10 min
  12. 12

    Views, Functions and Procedures

    Keep queries and logic inside the database

    1. 1Views4 min
    2. 2Save a Query as a ViewLab5 min
    3. 3Updatable Views4 min
    4. 4Change Data Through a ViewLab6 min
    5. 5Functions4 min
    6. 6Write Your Own FunctionsLab6 min
    7. 7PL/pgSQL4 min
    8. 8Decide in a FunctionLab6 min
    9. 9Stored Procedures4 min
    10. 10Save an Action as a ProcedureLab7 min
    11. 11Triggers4 min
    12. 12Log Every Price ChangeLab7 min
  13. 13

    Indexes and Performance

    Read query plans and make slow queries fast

    1. 1Query Plans4 min
    2. 2Read a Query PlanLab5 min
    3. 3CREATE INDEX4 min
    4. 4Make a Lookup FastLab6 min
    5. 5Multi-Column Indexes4 min
    6. 6Index Two ColumnsLab6 min
    7. 7What Indexes Cost4 min
    8. 8Weigh the Cost of IndexesLab7 min
    9. 9Unique Indexes3 min
    10. 10One Email per MemberLab5 min
    11. 11SQL Injection4 min
    12. 12Break and Fix a LoginLab6 min
  14. 14

    Transactions

    All or nothing, even when things fail

    1. 1BEGIN and COMMIT4 min
    2. 2Move Money in One TransactionLab5 min
    3. 3ROLLBACK3 min
    4. 4Undo a MistakeLab4 min
    5. 5Atomicity4 min
    6. 6All or NothingLab5 min
    7. 7ACID4 min
    8. 8Name the PropertyLab4 min
    9. 9SAVEPOINT4 min
    10. 10Keep the Good PartLab5 min
    11. 11Concurrency Problems4 min
    12. 12Replay a Lost UpdateLab5 min
    13. 13Isolation Levels4 min
    14. 14Choose an Isolation LevelLab4 min
    15. 15Locks4 min
    16. 16Lock Before You WithdrawLab4 min
  15. 15

    Database Security

    Roles, privileges and least privilege

    1. 1Roles4 min
    2. 2Create RolesLab4 min
    3. 3GRANT4 min
    4. 4Give the Support Team AccessLab5 min
    5. 5REVOKE3 min
    6. 6Take Privileges AwayLab5 min
    7. 7Column Privileges3 min
    8. 8Build the Phone BookLab5 min
    9. 9Views for Access3 min
    10. 10Share a Product ListLab5 min
    11. 11Role Membership4 min
    12. 12Build a Team RoleLab5 min
    13. 13Least PrivilegeLab8 min
  16. 16

    Beyond Relational

    JSON documents and when NoSQL fits

    1. 1JSON Columns4 min
    2. 2Add Product DetailsLab5 min
    3. 3Query JSON4 min
    4. 4Query the CartsLab6 min
    5. 5NoSQL Data Models4 min
    6. 6Tables or DocumentsLab6 min
    7. 7ACID, BASE and CAP4 min
    8. 8Two ReplicasLab5 min
    9. 9Choose a Database4 min
    10. 10Pick the Right StoreLab6 min