0% found this document useful (0 votes)
9 views9 pages

SQL CASE Statements for Student Grades

Uploaded by

aryaamble78
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)
9 views9 pages

SQL CASE Statements for Student Grades

Uploaded by

aryaamble78
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

PRACTICAL8

CREATE TABLE Students (


StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Grade CHAR(1)
);
INSERT INTO Students (StudentID, Name, Grade)
VALUES
(1, 'Alice' , 'A'),
(2, 'Bob' , 'B'),
(3, 'Charlie' , 'C'),
(4, 'David' , 'D'),
(5, 'Eva' , 'B'),
(6, 'Frank' , 'A');
1: Simple CASE Statement for Categorizing Grades
Q1. Write a query to classify students based on their grades. Return the student's name
and a performance label (Excellent, Good, Average, Needs Improvement) based on the
grade.
2: Searched CASE Statement for Grade Ranges
Q2. Assume grades have numeric equivalents (A=90, B=80, C=70, D=60). Write a query
that categorizes students into performance bands: High (90 and above), Medium (80-
89), Low (70-79), and Very Low (below 70).

3: Using CASE in a WHERE Clause


Q3. Write a query to select students who either have an A grade or a B grade. However, if
a student has a C grade, include them only if their name starts with 'C'.
4: CASE with Aggregate Functions
Q4. Write a query that calculates the count of students in each performance category
(Excellent, Good, Average, Needs Improvement).

5: Nested CASE Statements


Q5. Write a query that further categorizes students based on their performance:
Excellent if they have an 'A' grade, and Good if they have a 'B' grade. For all other grades,
categorize them based on whether their name starts with a vowel or a consonant.
6: Handling NULL Values with CASE
Q6. Modify the Students table by adding some NULL values in the Grade column. Write
a query to return 'No Grade Assigned' if the grade is NULL.
7: Using CASE in ORDER BY Clause
Q7. Write a query to sort students based on their performance category (Excellent first,
then Good, Average, and Needs Improvement last).
8: Complex CASE Statement with Multiple Conditions
Q8. Write a query to categorize students into three groups: Top Performers (grades 'A'
and 'B' with names starting with 'A' or 'B'), Mid Performers (grade 'C' or 'D'), and Others.
9: Using CASE with Date Functions
Q9. Suppose you add a column EnrollmentDate to the Students table. Write a query
that assigns 'New Student' to those enrolled in 2024, 'Experienced Student' to those
enrolled before 2024, and 'Future Enrollment' to those enrolled in 2025 or later.
10: Combining CASE with String Functions
Q10. Write a query that appends ' (A)' to names with an 'A' grade, ' (B)' to names with a
'B' grade, and so on. If the grade is NULL, append ' (No Grade)'.

Common questions

Powered by AI

Complex CASE statements handle multiple conditional checks. To group students as 'Top Performers' who have an A or B grade and names starting with 'A' or 'B', 'Mid Performers' with C or D grades, and 'Others', use: SELECT Name, CASE WHEN Grade IN ('A', 'B') AND Name LIKE 'A%' OR Name LIKE 'B%' THEN 'Top Performers' WHEN Grade IN ('C', 'D') THEN 'Mid Performers' ELSE 'Others' END AS Category FROM Students; This segmentation utilizes grade and name starting letters for classification.

Integrating DATE functions with CASE requires formatting for precise date comparisons. For categorizing students by enrollment years, you'd write: SELECT Name, EnrollmentDate, CASE WHEN YEAR(EnrollmentDate) = 2024 THEN 'New Student' WHEN YEAR(EnrollmentDate) < 2024 THEN 'Experienced Student' WHEN YEAR(EnrollmentDate) >= 2025 THEN 'Future Enrollment' END AS EnrollmentStatus FROM Students; This relies on YEAR function to extract the year for comparison, creating distinct enrollment categories.

Using a CASE statement in a WHERE clause is useful for conditional filtering based on multiple criteria. For example, to filter students with A or B grades, except for C grade students whose names start with 'C', the query would be: SELECT * FROM Students WHERE CASE WHEN Grade = 'A' THEN 1 WHEN Grade = 'B' THEN 1 WHEN Grade = 'C' AND Name LIKE 'C%' THEN 1 ELSE 0 END = 1;

Using CASE with string functions enables dynamic modifications based on conditions. To append grade letters to names, handle NULLs, use: SELECT Name + ' (' + CASE WHEN Grade IS NULL THEN 'No Grade' ELSE Grade END + ')' AS NameWithGrade FROM Students; This concatenation adjusts the displayed name by appending the grade or a placeholder if the grade is NULL, enriching result outputs with informative labels.

Nested CASE statements allow for additional layers of conditional logic. For example, to categorize students by grade and further by whether their name starts with a vowel or consonant, you could write: SELECT Name, CASE WHEN Grade = 'A' THEN 'Excellent' WHEN Grade = 'B' THEN 'Good' ELSE CASE WHEN Name LIKE '[AEIOU]%' THEN 'Starts with Vowel' ELSE 'Starts with Consonant' END END AS Category FROM Students; This allows sophisticated multi-level categorization based on various conditions.

A CASE statement can be used to create conditional logic in SQL queries. To classify students into performance levels based on grades, you can use a simple CASE statement. The SQL syntax is: SELECT Name, CASE WHEN Grade = 'A' THEN 'Excellent' WHEN Grade = 'B' THEN 'Good' WHEN Grade = 'C' THEN 'Average' WHEN Grade = 'D' THEN 'Needs Improvement' END AS PerformanceCategory FROM Students;

To handle NULL values in SQL, a CASE statement can be used to provide a default label. For grades, it would be: SELECT Name, CASE WHEN Grade IS NULL THEN 'No Grade Assigned' ELSE Grade END AS GradeStatus FROM Students; This substitutes NULL with a descriptive label in the results.

To count students in each performance category using CASE with aggregate functions, use a SELECT query with GROUP BY. Example: SELECT COUNT(*) AS Count, CASE WHEN Grade = 'A' THEN 'Excellent' WHEN Grade = 'B' THEN 'Good' WHEN Grade = 'C' THEN 'Average' WHEN Grade = 'D' THEN 'Needs Improvement' END AS Category FROM Students GROUP BY Category;

A simple CASE statement compares an expression to a set of simple expressions to find the result, whereas a searched CASE statement evaluates a set of Boolean expressions to determine the result. For categorizing grades with numeric equivalents, a searched CASE might look like this: SELECT Name, CASE WHEN Grade >= 90 THEN 'High' WHEN Grade BETWEEN 80 AND 89 THEN 'Medium' WHEN Grade BETWEEN 70 AND 79 THEN 'Low' ELSE 'Very Low' END AS PerformanceBand FROM Students;

A CASE statement in an ORDER BY clause allows sorting based on customized criteria. To sort students by performance category, the SQL could be: SELECT Name, CASE WHEN Grade = 'A' THEN 'Excellent' WHEN Grade = 'B' THEN 'Good' WHEN Grade = 'C' THEN 'Average' WHEN Grade = 'D' THEN 'Needs Improvement' END AS Category FROM Students ORDER BY CASE WHEN Grade = 'A' THEN 1 WHEN Grade = 'B' THEN 2 WHEN Grade = 'C' THEN 3 WHEN Grade = 'D' THEN 4 END;

You might also like