SQL practical guide

Below is a practical guide for SQL that aligns with the syllabus and covers key concepts such as database creation, table manipulation, data querying, and normalization. Each exercise includes a description, syllabus coverage, aim, and SQL code.

Exercise 8.1: Creating a Database

Description : Write SQL commands to create a new database and use it.
Syllabus Coverage : Data Definition Language (DDL), creating and using databases.
Aim : Introduce students to creating and managing databases.

Exercise 8.2: Creating Tables

Description : Write SQL commands to create a table with appropriate fields, data types, and constraints.
Syllabus Coverage : Creating tables, setting primary keys, and foreign keys.
Aim : Teach students how to define tables and enforce relationships.

Tabel Stucture

Exercise 8.3: Inserting Data

Description : Write SQL commands to insert sample data into the tables created in Exercise 8.2.
Syllabus Coverage : Data Manipulation Language (DML), inserting data.
Aim : Teach students how to populate tables with data.

Exercise 8.4: Querying Data

Description : Write SQL queries to retrieve data from a single table and multiple tables using joins.
Syllabus Coverage : Extracting rows and columns from a single table and multiple tables using inner join operations.
Aim : Teach students how to query data effectively.

Exercise 8.5: Updating and Deleting Data

Description : Write SQL commands to update and delete records in a table.
Syllabus Coverage : Modifying and deleting data.
Aim : Teach students how to modify and remove data from tables.

Exercise 8.6: Altering Table Structure

Description : Write SQL commands to add and delete columns, and modify constraints.
Syllabus Coverage : Altering tables (inserting and deleting attributes, adding and deleting foreign keys/primary keys).
Aim : Teach students how to modify table structures dynamically.

Exercise 8.7: Dropping Tables and Databases

Description : Write SQL commands to drop tables and databases.
Syllabus Coverage : Dropping tables and databases.
Aim : Teach students how to clean up resources.

Exercise 8.8: Normalization

Description : Normalize a given table to eliminate redundancies and anomalies.
Syllabus Coverage : Need for normalization, functional dependencies, levels of normalization (1NF, 2NF, 3NF).
Aim : Teach students how to design efficient and normalized databases.

Tabel Stucture1

Normalized Tables:

  1. Students Table :

Exercise 8.9: Functional Dependencies

Description : Identify functional dependencies in a given table and normalize it to 3NF.
Syllabus Coverage : Functional dependencies (full, partial, transitive), normalization levels.
Aim : Reinforce understanding of normalization principles.

tabel 3

Exercise 8.10: Aggregation and Grouping

Description : Write SQL queries to perform aggregation and grouping operations.
Syllabus Coverage : Advanced querying techniques.
Aim : Teach students how to summarize and analyze data.

Exercise 8.11: Subqueries

Description : Write SQL queries that use subqueries to retrieve data.
Syllabus Coverage : Advanced querying techniques.
Aim : Teach students how to use nested queries for complex data retrieval.

Exercise 8.12: Indexing

Description : Create an index on a table to improve query performance.
Syllabus Coverage : Performance optimization.
Aim : Introduce students to indexing for faster data retrieval.

Above exercises comprehensively cover SQL-related topics in your syllabus, including DDL , DML , normalization , aggregation , subqueries , and indexing . They are designed to progressively build students’ skills while aligning with the learning outcomes.

Below are advanced SQL exercises that go beyond the syllabus, exploring modern and advanced database concepts. These exercises will help you deepen your understanding of SQL and its capabilities in handling complex queries, optimization, and real-world applications.

Exercise 1: Advanced Joins

Description : Write SQL queries using advanced join types like LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
What You Learn : Advanced join techniques for combining data from multiple tables.
Code Example :

Exercise 2: Window Functions

Description : Use SQL window functions to perform calculations across a set of rows related to the current row.
What You Learn : Window functions like ROW_NUMBER(), RANK(), and DENSE_RANK().
Code Example :

Exercise 3: Common Table Expressions (CTEs)

Description : Use Common Table Expressions (CTEs) to simplify complex queries.
What You Learn : Writing reusable subqueries with CTEs.
Code Example :

Exercise 4: Recursive Queries

Description : Write recursive SQL queries to handle hierarchical data (e.g., organizational charts).
What You Learn : Recursive CTEs for querying hierarchical structures.
Code Example :

Exercise 5: JSON Data Handling

Description : Work with JSON data stored in a relational database.
What You Learn : Querying and manipulating JSON fields in SQL.
Code Example :

Exercise 6: Index Optimization

Description : Analyze query performance and create indexes to optimize slow queries.
What You Learn : Indexing strategies and query optimization.
Code Example :

Exercise 7: Stored Procedures

Description : Write a stored procedure to encapsulate a complex business logic.
What You Learn : Creating reusable database logic with stored procedures.
Code Example :

Exercise 8: Triggers

Description : Write a trigger to automatically log changes to a table.
What You Learn : Automating actions with triggers.
Code Example :

Exercise 9: Transactions

Description : Use transactions to ensure atomicity in multi-step operations.
What You Learn : Managing database transactions for consistency.
Code Example :

Exercise 10: Materialized Views

Description : Create a materialized view to store the results of a complex query for faster access.
What You Learn : Materialized views for performance optimization.
Code Example :

Exercise 11: Partitioning

Description : Partition a large table to improve query performance.
What You Learn : Table partitioning for scalability.
Code Example :

Exercise 12: Full-Text Search

Description : Implement full-text search for efficient text-based queries.
What You Learn : Searching unstructured text data efficiently.
Code Example :

These exercises explore advanced SQL concepts such as window functions , recursive queries , JSON handling , stored procedures , triggers , transactions , materialized views , partitioning , and full-text search . They provide practical, real-world applications of SQL and will help you explore areas beyond the syllabus.

Spread the love