Querying data using SQL


Here’s the Database Diagram for the SQL Exercises.






    1. Creating Primary and Foreign keys in existing tables

      • Creating a primary key


      • Creating a foreign key


    2. How to SELECT column names in a table

      • SQL:

      • Result:

    3. AGGREGATIONS: SELECT aggregated and non-aggregated columns from a table

      • Task: List the total Staffing Entitlements for each school.

      • Source table: [Staffing]
        Note how the same school appears in multiple rows.



      • SQL:



      • Result: [Total Staffing Entitlement] is aggregated by School Code.



      • Alternative: The same result can be obtained by using a shorter query shown below.



    4. ORDER BY vs HAVING

      • SQL:

      • Result:


    5. SUBQUERIES

      • SQL:

      • Result:




      • SQL:

      • Result:


    6. HOW TO INSERT, UPDATE AND DELETE DATA

      • SQL:

      • Result:


      • SQL:

      • Result:


      • SQL:

      • Result: