0% found this document useful (0 votes)
20 views2 pages

SQL Queries for Student Grades Analysis

The document contains SQL query exercises based on a university schema, including tasks such as retrieving course titles, student IDs, instructor salaries, and enrollment figures. It also includes operations for calculating grade points and averages, as well as performing inserts, deletes, and updates. Additionally, it outlines how to assign grades based on student scores from a marks relation.

Uploaded by

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

SQL Queries for Student Grades Analysis

The document contains SQL query exercises based on a university schema, including tasks such as retrieving course titles, student IDs, instructor salaries, and enrollment figures. It also includes operations for calculating grade points and averages, as well as performing inserts, deletes, and updates. Additionally, it outlines how to assign grades based on student scores from a marks relation.

Uploaded by

jtijerina8208
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SQL Query Exercises

1. Write the following queries in SQL, using the university schema.


a. Find the titles of courses in the [Link]. Department that have 3 credit hours.
SELECT title
FROM course
WHERE depart_name = ‘Comp. Sci.’
AND credits = 3

b. Find the IDs of all students who were taught by an instructor named Einstein; make sure
there are no duplicates in the result.
SELECT DISTINCT [Link]
From (student join takes using(ID))
using(course_id, sec_id, semester, year)
WHERE [Link] = ‘Einstein’

c. Find the highest salary of any instructor.


SELECT MAX salary
FROM instructor

d. Find all instructors earning the highest salary.


SELECT name
FROM instructors
WHERE salary = (select max(salary) from instructor

e. Find the enrollment of each section that was offered in the Fall 2009.

f. Find the maximum enrollment, across all sections, in Fall 2009.

g. Find the sections that had the maximum enrollment in Fall 2009.

2. Suppose you are given a relation grad_poins(grade, points) which provides a conversion from letter
grades in the takes relation to numeric score; for example an “A” grade could be specified to correspond
to 4 points , and “A-” to 3.7 points, a “ B+” to 3.3 points, a “B” to 3 points, and so on. The grades earned
by a student for a course offering (section) is defined as the number of credits for the course multiplied
by the numeric points for the grade that the student received.

Give the above relation, and our university schema, write each of the following queries in SQL. You can
assume for simplicity that no takes tuple has null value for grade.
a. Find the total grade-points earned by the student with ID 12345, across all course taken by the
student.
b. Find the grade-point average (GPA) for the above student, that is, the total grade-points divided
by the total credits for the associated courses.
c. Find the ID and the grade-point average of every student.
3. Write the following inserts, deletes or updates in SQL, using the university schema.
a. Increase the salary of each instructor in the [Link]. department by 10%.
b. Delete all courses that have never been offered. (that is, do not occur in the section relation)
c. Insert every student whose tot_cred attribute is greater than 100 as an instructor in the same
department, with a salary of $10,000.

4. Suppose that we have a relation marks(ID, score) and we wish to assign grades to students based on
the score as follows: grade F i score < 40, grade C if 40<= score < 60, grade B if 60<= score< 80, and grade
A if 80<=score. Write SQL queries to do the following:
a. Display the grade for each student, based on the marks relation.
b. Find the number of students with each grade.

Common questions

Powered by AI

To determine the highest salary among instructors, you use the SQL query: SELECT MAX(salary) FROM instructor. SQL facilitates this query by providing aggregate functions like MAX(), which can easily calculate the highest value within a specified column .

You would use a SQL query that joins multiple tables to find student IDs without duplicates: SELECT DISTINCT student.ID FROM (student JOIN takes USING(ID)) JOIN teaches USING(course_id, sec_id, semester, year) WHERE instructor.name = 'Einstein'. This query involves joining the 'student', 'takes', and 'teaches' tables, reflecting a normalized data structure where teaching information is separate from student course records .

To assign grades based on scores, use a CASE statement: SELECT ID, CASE WHEN score < 40 THEN 'F' WHEN score BETWEEN 40 AND 59 THEN 'C' WHEN score BETWEEN 60 AND 79 THEN 'B' WHEN score >= 80 THEN 'A' END AS grade FROM marks. This reflects SQL's ability to process conditional logic within queries using the CASE statement to classify data based on criteria .

To delete courses never offered in any section, you would execute: DELETE FROM course WHERE course_id NOT IN (SELECT course_id FROM section). This SQL approach ensures data integrity by only removing courses that do not appear in the 'section' table, illustrating how foreign key constraints and subqueries can prevent deletion of related data that should remain intact .

To find a student's GPA, calculate the total grade-points and divide by total credits using SQL: (SELECT SUM(course.credits * grad_points.points) FROM takes JOIN course USING(course_id) JOIN grad_points ON takes.grade = grad_points.grade WHERE takes.ID = 12345) / (SELECT SUM(credits) FROM takes JOIN course USING(course_id) WHERE takes.ID = 12345). SQL simplifies this by allowing nested subqueries and mathematical operations directly within the SELECT clause .

To find the titles of all 3-credit courses offered by the 'Comp. Sci.' department, you would use the query: SELECT title FROM course WHERE depart_name = 'Comp. Sci.' AND credits = 3. This query reveals that the database has a table named 'course' with fields including 'title', 'depart_name', and 'credits', which allows for filtering specifically by department and credit value .

You calculate a student's total grade-points by linking the 'takes' table with 'grad_points' and using: SELECT SUM(course.credits * grad_points.points) FROM takes JOIN course USING(course_id) JOIN grad_points ON takes.grade = grad_points.grade WHERE takes.ID = 12345. This involves understanding the database structure, whereby you join courses, grades, and their respective points to compute an aggregate total .

To increase salaries by a percentage, use UPDATE with a calculation: UPDATE instructors SET salary = salary * 1.10 WHERE depart_name = 'Comp. Sci.'. This query highlights the use of SQL to perform arithmetic operations directly within the update command to adjust data based on defined criteria .

To find all instructors earning the maximum salary, you would use: SELECT name FROM instructors WHERE salary = (SELECT MAX(salary) FROM instructor). This illustrates the use of subqueries in SQL, where a subquery can provide a value (in this case, the maximum salary) to filter results in a main query .

To identify sections with maximum enrollment in Fall 2009, you can use: SELECT section_id FROM enrollment WHERE semester = 'Fall' AND year = 2009 AND enrollment = (SELECT MAX(enrollment) FROM enrollment WHERE semester = 'Fall' AND year = 2009). This query employs nested subqueries to first compute the maximum enrollment, and then find all sections with this enrollment, demonstrating advanced query structuring .

You might also like