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

SQL Test

sql test

Uploaded by

Yatin Singhal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SQL Test

sql test

Uploaded by

Yatin Singhal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#### Section 1: Database and Table Creation

1. **Create a Database**
- Create a database named `School`.

2. **Create Tables**
- Create a table named `Students` with the following columns:
- `StudentID` (Primary Key, Integer)
- `FirstName` (VARCHAR(50))
- `LastName` (VARCHAR(50))
- `DateOfBirth` (DATE)

3. **Create another table named `Courses` with the following columns:**


- `CourseID` (Primary Key, Integer)
- `CourseName` (VARCHAR(100))
- `Credits` (Integer)

4. **Create a table named `Enrollments` with the following columns:**


- `EnrollmentID` (Primary Key, Integer)
- `StudentID` (Foreign Key, Integer)
- `CourseID` (Foreign Key, Integer)
- `EnrollmentDate` (DATE)

#### Section 2: Inserting Data

5. **Insert Data into `Students` Table**


- Insert at least 3 students into the `Students` table.

6. **Insert Data into `Courses` Table**


- Insert at least 3 courses into the `Courses` table.

7. **Insert Data into `Enrollments` Table**


- Enroll the students in different courses with appropriate dates.

#### Section 3: Basic Queries

8. **Retrieve all Students**


- Write a query to retrieve all records from the `Students` table.
9. **Retrieve all Courses**
- Write a query to retrieve all records from the `Courses` table.

10. **Retrieve Student Enrollments**


- Write a query to retrieve the `StudentID` and `CourseID` for all enrollments.

11. **Join Query**


- Write a query to retrieve the `FirstName`, `LastName`, and `CourseName` for all students along
with the courses they are enrolled in.

12. **Count Enrollments**


- Write a query to count the number of students enrolled in each course.

13. **Filter by Date**


- Write a query to find all students enrolled after a specific date.

#### Section 4: Updating and Deleting Data

14. **Update Student Information**


- Write a query to update the last name of a student.

15. **Delete a Record**


- Write a query to delete a student from the `Students` table.

16. **Remove Course Enrollment**


- Write a query to remove a specific course enrollment.

17. **String Functions**


- Write a query to concatenate the first and last names of students from the `Students` table.
- Write a query to find the length of the first name of students.

18. **Numeric Functions**


- Write a query to round the credits of courses to the nearest whole number.
- Write a query to find the absolute value of a number and the remainder of a division (MOD).

19. **Date and Time Functions**


- Write a query to extract the year from the `DateOfBirth` of students.
- Write a query to find the current date and time.
20. **Aggregate Functions**
- Write a query to count the number of students.
- Write a query to find the average credits of all courses.

21. **Conditional Functions**


- Write a query to use the `CASE` statement to categorize students based on their age.
- Write a query to use `COALESCE` to handle NULL values in a column.

---

### Module 5: Working with Multiple Tables

22. **Understanding Table Joins**


- Write a query to perform an INNER JOIN between `Students` and `Enrollments`.
- Write a query to perform a LEFT JOIN between `Courses` and `Enrollments`.

23. **Combining Results**


- Write a query to combine results of two select statements using UNION.
- Write a query to find common elements between two select statements using INTERSECT.

24. **Nested Queries and Subqueries**


- Write a subquery to find students enrolled in a specific course.
- Write a nested query to find the student with the maximum enrollment date.

25. **Set Operations**


- Write a query to use UNION ALL to combine results of two select statements.
- Write a query to use EXCEPT to find records in one select statement but not in another.

---

### Module 6: SQL Constraints and Indexes

26. **Defining Constraints**


- Write a query to add a UNIQUE constraint to the `Email` column in the `Students` table.
- Write a query to add a CHECK constraint to ensure credits are positive in the `Courses` table.

27. **Implementing Indexes**


- Write a query to create an index on the `LastName` column in the `Students` table.
- Write a query to drop an existing index.

28. **Working with Views**


- Write a query to create a view that lists all student enrollments.
- Write a query to alter the view created in the previous question.

29. **Sequences and Identity Columns**


- Write a query to create a sequence for generating unique `StudentID`.
- Write a query to use an identity column for auto-incrementing `CourseID`.

---

### Module 7: Advanced SQL Topics

30. **Stored Procedures and Functions**


- Write a stored procedure to insert a new student into the `Students` table.
- Write a function to calculate the age of a student based on their `DateOfBirth`.

31. **Triggers**
- Write a trigger to automatically update a student's enrollment count upon a new enrollment.
- Write a trigger to log changes in the `Courses` table.

32. **Cursors**
- Write a query to declare a cursor for retrieving student names.
- Write a query to use the cursor to fetch and print student names.

33. **Handling Errors and Exceptions**


- Write a query to handle exceptions in a stored procedure.
- Write a query to use TRY...CATCH to handle division by zero errors.

34. **Dynamic SQL and Parameterized Queries**


- Write a query to execute dynamic SQL.
- Write a parameterized query to select students based on a given age.

---

### Module 8: Data Control and Security

35. **User Management**


- Write a query to create a new user with a password.
- Write a query to delete an existing user.

36. **Granting and Revoking Permissions**


- Write a query to grant SELECT permission on the `Students` table to a user.
- Write a query to revoke INSERT permission on the `Courses` table from a user.

37. **Implementing Security Policies**


- Write a query to create a security policy that restricts access to students' data based on their
age.

38. **Auditing Database Activities**


- Write a query to enable auditing for the `Students` table.
- Write a query to retrieve audit logs.

---

40. **Indexing Strategies and Performance Tuning**


- Write a query to analyze the impact of an index on query performance.

41. **Analyzing Query Execution Plans**


- Write a query to generate an execution plan for a complex SELECT statement.

42. **Utilizing EXPLAIN and Analyzing Query Performance**


- Write a query to use EXPLAIN to understand query performance issues.

---

You might also like