By the end of this chapter, you will be able to:
Mastering these SQL skills will empower you to handle real-world databases confidently, making you a valuable asset in any data-driven workplace.
Structured Query Language (SQL) is a fundamental skill for computer science professionals in Kenya who work with databases, enabling them to manage and manipulate data efficiently. As organizations increasingly rely on digital data for decision-making, proficiency in SQL is essential for designing, querying, and maintaining databases that support business operations. This chapter explores SQL’s definition, its defining characteristics, and the core components that form the basis of database interactions in professional environments such as banks, universities, and government agencies.
SQL is the standard language used to communicate with relational database management systems (RDBMS). It allows users to perform a variety of operations including data retrieval, insertion, update, and deletion. Understanding SQL is crucial for computer science professionals in Kenya, especially when working with systems in sectors like finance, healthcare, and public administration, where accurate data handling is critical.
SQL stands for Structured Query Language, a specialized programming language designed for managing relational databases. It provides a standardized way to create, read, update, and delete data stored in tables, which are organized into rows and columns. Unlike general-purpose programming languages, SQL focuses exclusively on data manipulation and querying within database systems.
SQL emerged in the 1970s as a result of research on relational database models, and it has since become the universal language supported by most commercial and open-source database systems. In Kenya, institutions such as the Kenya Revenue Authority (KRA) and commercial banks rely heavily on SQL to manage large volumes of transactional data securely and efficiently.
Declarative Language: SQL is declarative, meaning users specify what data they want without detailing how to retrieve it. This simplifies database operations, allowing professionals to focus on data requirements rather than procedural details.
Non-Procedural Nature: Unlike procedural languages, SQL commands do not require specifying control flow. This facilitates quicker learning and application for database tasks such as querying customer records in a SACCO.
Portability: SQL commands are largely portable across different database systems, meaning that skills learned using MySQL or PostgreSQL can be applied in Microsoft SQL Server or Oracle environments with minimal adjustments.
Set-Based Operations: SQL works on sets of data rather than individual records, enabling efficient processing of large datasets, such as generating reports from a county government’s population database.
Extensibility: SQL supports extensions and procedural elements (like PL/SQL in Oracle), allowing developers to write complex logic while still leveraging standard querying capabilities.
SQL’s defining features enable it to serve as the backbone of data management in professional Kenyan contexts. These characteristics ensure that SQL remains versatile, powerful, and accessible to database administrators and developers.
SQL focuses on what data is needed rather than how to obtain it, which means users write queries specifying the desired outcome. This contrasts with imperative programming languages and reduces the complexity of database interactions.
This approach allows professionals in financial institutions like Equity Bank to write queries extracting customer transaction histories without managing underlying data retrieval processes.
Declarative syntax improves productivity since less code is required to perform complex data operations.
SQL encompasses both Data Manipulation Language (DML) and Data Definition Language (DDL) components, allowing users to handle data content and database schema respectively.
DML commands such as SELECT, INSERT, UPDATE, and DELETE enable modification and retrieval of data records in tables.
DDL commands like CREATE, ALTER, and DROP allow database designers at universities to define or modify table structures and relationships.
SQL supports transaction management to ensure data integrity during multi-step operations, essential in environments where consistency is critical.
Commands such as BEGIN TRANSACTION, COMMIT, and ROLLBACK help maintain accurate records in scenarios like booking hotel reservations or processing loan applications at SACCOs.
This capability ensures that either all changes are applied successfully or none, protecting against partial updates that could corrupt data.
SQL supports constraints such as primary keys, foreign keys, unique constraints, and checks to enforce data validity.
For example, a county government database might use foreign key constraints to ensure that employee records always reference valid department entries.
These features prevent erroneous data entry and maintain relational consistency across tables.
SQL includes commands to define user roles, privileges, and permissions, enabling fine-grained control over who can access or modify data.
In banking systems, this ensures that tellers can only access customer account information relevant to their duties, while auditors have read-only access for compliance checks.
SQL’s security features are fundamental for protecting sensitive data in compliance with Kenya’s Data Protection Act.
SQL’s functionality is organized into distinct components that collectively manage data definition, manipulation, control, and querying. Mastery of these components enables computer science professionals to design robust database applications.
DDL commands define and modify database schema objects such as tables, indexes, and views.
CREATE: Defines new tables or database objects. For instance, a developer at a retail business might create a table to store product inventory details.
ALTER: Modifies existing table structures, such as adding new columns or changing data types, which is common during system upgrades.
DROP: Deletes tables or objects that are no longer needed, helping maintain database hygiene.
TRUNCATE: Removes all rows from a table quickly without logging individual row deletions, useful for clearing temporary data in test environments.
COMMENT: Adds descriptive text to database objects, aiding documentation and maintenance.
DML commands handle the management of data within tables.
SELECT: Retrieves data from one or more tables, supporting filtering, sorting, and aggregation. For example, a university registrar uses SELECT queries to generate student grade reports.
INSERT: Adds new rows of data into tables, such as entering new customer records in a SACCO database.
UPDATE: Modifies existing data, like changing the status of loan applications after approval.
DELETE: Removes records, for instance deleting outdated inventory entries in a supermarket database.
MERGE: Combines data from source tables into a target table, used in data warehousing scenarios.
DCL commands regulate access to data and database operations.
GRANT: Provides specific privileges to users or roles, such as allowing a bank clerk to update customer profiles.
REVOKE: Removes previously granted permissions, essential for maintaining security when users change roles.
DENY: Explicitly denies permissions, overriding granted privileges in some database systems.
These commands help institutions like county governments comply with data access policies.
TCL commands manage database transactions to ensure data consistency and integrity.
BEGIN TRANSACTION: Marks the start of a transaction block where multiple operations are treated as a single unit.
COMMIT: Saves all changes made during the transaction permanently to the database.
ROLLBACK: Reverts all changes made during the transaction if errors occur.
SAVEPOINT: Creates intermediate points within a transaction for partial rollbacks.
These commands are vital in financial applications such as loan disbursement systems to prevent data corruption.
SQL queries are constructed using various clauses that specify data retrieval conditions and manipulations.
FROM: Specifies the tables to query from.
WHERE: Filters rows based on conditions.
GROUP BY: Aggregates data by one or more columns.
HAVING: Filters groups based on aggregate conditions.
ORDER BY: Sorts the result set.
For example, a data analyst at a county health department might write a query using these clauses to analyze disease incidence by region and month.
Define Structured Query Language (SQL) and explain its role in database management systems. (5 marks)
Describe five key characteristics of SQL that make it suitable for managing relational databases. (10 marks)
Explain the main components of SQL and provide two examples of commands under each component. (15 marks)
Discuss how transaction control commands in SQL contribute to data integrity, illustrating with an example from the banking sector. (10 marks)
Create a free account to open more of this chapter.
Free: practical guides, quick cards, workplace scenarios and more.
Create a free accountThis chapter introduced Structured Query Language, commonly known as SQL, defining it as the standard language used for managing and manipulating relational databases. The discussion covered its key characteristics and essential components that enable users to define, manipulate, and control data effectively. Data definition queries were explored in detail, focusing on the CREATE, DROP, and ALTER statements, which are fundamental for creating tables, removing them, and modifying their structure. The chapter provided a demonstration of the CREATE TABLE statement and explained various table constraints such as PRIMARY KEY, FOREIGN KEY, NOT NULL, CHECK, UNIQUE, and DEFAULT, which enforce data integrity. Editing table schemas using the ALTER statement was examined through examples of adding, dropping, and modifying attributes. The DROP TABLE statement was also covered as a means to delete entire tables from the database. Data manipulation query statements including INSERT, SELECT, UPDATE, and DELETE were explained with practical detail on how to retrieve, insert, update, and delete records. Finally, the concept of SQL joins was introduced, defining what joins are and describing their types, followed by exercises on creating and querying databases from validated ER models and constructing simple join queries.
Type: Individual
| Tools & Equipment | Materials |
|---|---|
| Functional computer installed with MS SQL Server | Plain A4 paper |
| S/N | Item | Quantity |
|---|---|---|
| 1 | Functional computer installed with MS SQL Server | 1 Pc per Candidate |
| 2 | Plain A4 paper | 2 Pcs per Candidate |
| Items to be Evaluated | Marks Available | Marks Obtained | Comments |
|---|---|---|---|
| TASK 1: Create Database and Tables | |||
| Launched MS SQL Server correctly (Award 2 marks or zero) | 2 | ||
| Created database named StudentDB (Award 1 mark for correct database name) | 1 | ||
| Created table stDetails with fields: stID (Primary Key), stName, stGender, stCourse (Award 1 mark each for correct table name and correct fields) | 2 | ||
| Created table stMarks with fields: stID (Foreign Key), subject, marks (Award 2 marks for correct table and fields including foreign key) | 2 | ||
| Sub-Total | 7 | ||
| TASK 2: Insert Records and Update Tables | |||
| Inserted at least 5 records into stDetails with valid data (Award 2 marks for complete and valid insertions) | 2 | ||
| Inserted at least 10 records into stMarks with valid subject and marks data linked to stDetails (Award 2 marks for complete and valid insertions) | 2 | ||
| Added a 'total' column to stMarks table (Award 1 mark for successful ALTER TABLE statement) | 1 | ||
| Added an 'average' column to stMarks table (Award 1 mark for successful ALTER TABLE statement) | 1 | ||
| Calculated total marks per student and updated 'total' field (Award 2 marks for correct SQL update with sum aggregation) | 2 | ||
| Calculated average marks per student and updated 'average' field (Award 2 marks for correct SQL update with average aggregation) | 2 | ||
| Sub-Total | 10 | ||
| TASK 3: Query Data | |||
| Extracted list of male students enrolled in 'Computer Science' course (Award 2 marks for correct SELECT with WHERE conditions) | 2 | ||
| Extracted list of students scoring above 75 marks in 'Mathematics' (Award 2 marks for correct SELECT with JOIN and WHERE conditions) | 2 | ||
| Extracted students with average marks below 50 (Award 2 marks for correct use of WHERE on average field) | 2 | ||
| Created an index on stID field in stDetails table (Award 1 mark for correct CREATE INDEX statement) | 1 | ||
| Sub-Total | 7 | ||
| PRODUCT CHECKLIST | |||
| Database StudentDB exists with correctly structured tables stDetails and stMarks (Award 3 marks for correct table structures and relationships) | 3 | ||
| Tables populated with at least the minimum required valid records (Award 3 marks for completeness and correctness of inserted data) | 3 | ||
| Total and average fields correctly calculated and updated in stMarks table (Award 4 marks for accuracy of calculations and stored values) | 4 | ||
| SQL queries return correct and expected results as per task requirements (Award 5 marks for correct query outputs) | 5 | ||
| Index on stID field created and functional (Award 3 marks for correct index creation and functionality) | 3 | ||
| Sub-Total | 18 | ||
| GRAND TOTAL | 42 | ||
Type: Individual
| Tools & Equipment | Materials |
|---|---|
| Functional computer installed with MS SQL Server | Plain A4 paper |
| S/N | Item | Quantity |
|---|---|---|
| 1 | Functional computer installed with MS SQL Server | 1 Pc per Candidate |
| 2 | Plain A4 paper | 2 Sheets per Candidate |
| Items to be Evaluated | Marks Available | Marks Obtained | Comments |
|---|---|---|---|
| TASK 1: Database and Tables Creation | |||
| Launched MS SQL Server correctly (Award 2 marks or zero) | 2 | ||
| Created database named StudentDB (Award 3 marks or zero) | 3 | ||
| Created table StudentDetails with fields StudentID (Primary Key), FirstName, LastName, Gender, and Class (Award 4 marks or zero) | 4 | ||
| Created table StudentScores with fields ScoreID (Primary Key), StudentID (Foreign Key), Subject, and Score (Award 4 marks or zero) | 4 | ||
| Sub-Total | 13 | ||
| TASK 2: Data Insertion | |||
| Inserted at least 5 records into StudentDetails table (Award 1 mark per correct record, max 4) | 4 | ||
| Inserted at least 10 records into StudentScores table with valid StudentID references (Award 1 mark per correct record, max 5) | 5 | ||
| Sub-Total | 9 | ||
| TASK 3: SQL Querying | |||
| Extracted list of male students in Class 3A (Award 3 marks or zero) | 3 | ||
| Extracted list of students who scored more than 70 in Mathematics (Award 3 marks or zero) | 3 | ||
| Extracted average score per subject from StudentScores (Award 4 marks or zero) | 4 | ||
| Created a view named HighScorers for students scoring above 80 in any subject (Award 4 marks or zero) | 4 | ||
| Sub-Total | 14 | ||
| PRODUCT CHECKLIST | |||
| Database structure correctly created with required tables and fields (Award 6 marks or zero) | 6 | ||
| Data accurately inserted with valid records and referential integrity (Award 6 marks or zero) | 6 | ||
| SQL queries executed correctly producing accurate results (Award 6 marks or zero) | 6 | ||
| Sub-Total | 18 | ||
| GRAND TOTAL | 54 | ||
At the start of this chapter we promised you would be able to:
Tick each one you can genuinely do.
So, are you there yet?
You're competent when you can confidently do 50% or more of what this chapter promised.
Sign in to record how you're doing.