Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

SQL Assignment 2

Uploaded by

Shahwaiz Niazi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

SQL Assignment 2

Uploaded by

Shahwaiz Niazi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL Assignment 2: Step-by-Step Guide for Students

Objective:

 Create a database, create tables within the database, insert data, and perform SELECT
queries.

Step 1: Creating a Database

1. Create a new database called SchoolDB. The database should be the central place for
all your tables and data.
2. Select the database you just created to work in it.

Step 2: Creating Tables

1. Design tables based on your requirements. For a simple school database, you can
create the following tables:
o Students Table: Store details like student name, age, gender, and grade.
o Courses Table: Store course details like course name, course code, and
instructor.
o Enrollments Table: Store information about which students are enrolled in
which courses.

You must decide on appropriate data types for each column (e.g., VARCHAR, INT,
DATE).

2. Define relationships between tables. For instance:


o A student can enroll in multiple courses, and each course can have multiple
students. This relationship will require the creation of a linking table (e.g.,
Enrollments).

Step 3: Inserting Data

1. Insert sample data into each table. For example, you could add 5-10 students, 3-5
courses, and 10-15 enrollment records. Each entry should reflect realistic data.
2. Make sure to use valid data types for each field and ensure that the relationships (such
as foreign keys) are respected.

Step 4: Performing SELECT Queries


Once you have created the database, tables, and inserted data, perform various SELECT
queries to retrieve information. Here are some examples:

1. Simple SELECT: Retrieve all records from a single table.


o Example: Show all students in the Students table.
2. SELECT with WHERE: Filter records based on specific conditions.
o Example: Show all students whose grade is above 80.
3. SELECT with ORDER BY: Retrieve records and sort them based on one or more
columns.
o Example: List courses sorted by course name or instructor.
4. SELECT with JOIN: Combine data from multiple tables using a join.
o Example: Retrieve a list of students and their enrolled courses by joining the
Students table with the Enrollments and Courses tables.
5. SELECT with COUNT(): Get the count of records that match a certain condition.
o Example: Count how many students are enrolled in a particular course.
6. SELECT with GROUP BY: Group results by a specific column and perform
aggregate functions like COUNT, AVG, or SUM.
o Example: Group students by grade and count how many students are in each
grade.
7. SELECT with LIKE: Use pattern matching to filter data.
o Example: Find students whose names start with the letter "A".
8. SELECT with DISTINCT: Retrieve unique records (no duplicates).
o Example: Get a list of all distinct courses that students are enrolled in.
9. SELECT with LIMIT: Limit the number of records returned.
o Example: Show the top 5 students with the highest grades.
10. SELECT with INNER JOIN and Aggregation: Combine data from multiple tables
and perform calculations.
o Example: Show the total number of students in each course by joining
Enrollments with Courses and using COUNT.

You might also like