0% found this document useful (0 votes)
0 views

Step-by-Step Practical Exercise_ SQL

This document outlines a step-by-step practical exercise for learning SQL, covering basic operations such as setting up a database environment, executing SQL commands, and managing data. It includes assessment criteria with a total of 40 marks, detailing tasks like creating databases and tables, inserting, updating, and deleting records, and running queries. The exercise culminates in creating reusable queries and requires documentation of SQL commands and results for submission.

Uploaded by

leetoto44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Step-by-Step Practical Exercise_ SQL

This document outlines a step-by-step practical exercise for learning SQL, covering basic operations such as setting up a database environment, executing SQL commands, and managing data. It includes assessment criteria with a total of 40 marks, detailing tasks like creating databases and tables, inserting, updating, and deleting records, and running queries. The exercise culminates in creating reusable queries and requires documentation of SQL commands and results for submission.

Uploaded by

leetoto44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Step-by-Step Practical Exercise: SQL

Exercise Overview

This exercise will guide you through the basic operations in SQL, including starting and working
in the SQL environment, executing basic SQL commands, running SQL statements, displaying
data sets, sorting, searching, filtering databases, and using/reusing queries.

Assessment Criteria

1. Starting and Working in the SQL Environment (4 marks)


2. Basic SQL Code (SELECT, UPDATE, DELETE) (8 marks)
3. Run SQL Statements (8 marks)
4. Draw Display Data Sets (8 marks)
5. Sort, Search, and Filtering Database (4 marks)
6. Queries (Use/Reuse) (8 marks)
○ Total Marks: 40

Step-by-Step Instructions

Step 1: Starting and Working in the SQL Environment (4 marks)

1. Install and Set Up a Database Environment:


○ Install a database management system (DBMS) like MySQL, PostgreSQL, or
SQLite. For this exercise, we'll use MySQL.
○ Download and install MySQL from the official website.
○ Start the MySQL server and open the MySQL Command Line Client or MySQL
Workbench.
2. Create a Database:

Open the SQL environment and execute the following command to create a database:
sql

CREATE DATABASE SchoolDB;

Switch to the newly created database:


Sql
USE SchoolDB;


Step 2: Basic SQL Code (SELECT, UPDATE, DELETE) (8 marks)

1. Create Tables:

Create Students and Classes tables:


sql

CREATE TABLE Classes (


ClassID INT AUTO_INCREMENT PRIMARY KEY,
ClassName VARCHAR(50)
);

CREATE TABLE Students (


StudentID INT AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DateOfBirth DATE,
ClassID INT,
FOREIGN KEY (ClassID) REFERENCES Classes(ClassID)
);


2. Insert Data:

Insert records into the Classes table:


sql

INSERT INTO Classes (ClassName) VALUES ('Math'), ('Science'),


('History');

Insert records into the Students table:


sql

INSERT INTO Students (FirstName, LastName, DateOfBirth, ClassID)


VALUES
('John', 'Doe', '2000-01-01', 1),
('Alice', 'Smith', '2001-02-02', 1),
('Bob', 'Jones', '2002-03-03', 2),
('Carol', 'White', '2003-04-04', 2),
('David', 'Brown', '2004-05-05', 3);

3. Select Data:

Retrieve all records from the Students table:


sql

SELECT * FROM Students;


4. Update Data:

Update the last name of a student:


sql

UPDATE Students
SET LastName = 'Johnson'
WHERE FirstName = 'John' AND LastName = 'Doe';


5. Delete Data:

Delete a student record:


sql

DELETE FROM Students


WHERE FirstName = 'David' AND LastName = 'Brown';

Step 3: Run SQL Statements (8 marks)

1. Execute the above SQL Statements:


○ Use the SQL environment to run each of the above SQL commands (CREATE,
INSERT, SELECT, UPDATE, DELETE) and observe the changes in the
database.

Step 4: Draw Display Data Sets (8 marks)

1. Retrieve and Display Data:

Write and execute a query to display the full list of students along with their class names:
sql

SELECT Students.StudentID, Students.FirstName, Students.LastName,


Classes.ClassName
FROM Students
JOIN Classes ON Students.ClassID = Classes.ClassID;

Step 5: Sort, Search, and Filtering Database (4 marks)

1. Sort Data:

Sort students by last name in ascending order:


sql

SELECT * FROM Students


ORDER BY LastName ASC;


2. Search and Filter Data:

Filter students born after 2002:


sql

SELECT * FROM Students


WHERE DateOfBirth > '2002-01-01';

Step 6: Queries (Use/Reuse) (8 marks)

1. Create and Save a Query:

Create a reusable query to get students in a specific class:


sql

CREATE VIEW StudentsInMath AS


SELECT FirstName, LastName
FROM Students
WHERE ClassID = (SELECT ClassID FROM Classes WHERE ClassName =
'Math');


2. Use the Saved Query:

Retrieve data from the saved query:


sql
SELECT * FROM StudentsInMath;

Final Steps

● Review your work to ensure all criteria are met.


● Document your SQL commands and results to show the steps taken and the
outcomes.
● Submit your SQL scripts and results for assessment.

You might also like