bacs 287 structured query language 2. bacs 485 sql practice problems assume that a database named...

21
BACS 287 Structured Query Language 2

Upload: lewis-campbell

Post on 18-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 287

Structured Query Language 2

Page 2: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Practice Problems Assume that a database named

COLLEGE exists. It contains the tables defined below.

STUDENT(SSN, Lname, Fname, Class, BDate, Major, GPA)

TEACHER(FacNum, Name, Dept, Title, Salary)

CLASS(ClassNum, ClassName, Time, Credits, FacNum)

ENROLL(ClassNum,SSN, Grade)

Page 3: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

E/R DiagramSTUDENT

ENROLL

CLASS

TEACHES

TEACHER

Page 4: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Practice 1 Write a valid SQL query to retrieve

and display the last name, major, and GPA of all students.

Page 5: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Answer 1 Write a valid SQL query to retrieve

and display the last name, major, and GPA of all students.

SELECT Lname, Major, GPAFROM STUDENT;

Page 6: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Practice 2 Write a valid SQL query to retrieve

and display the SSN, last name, and classification of all math majors.

Page 7: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Answer 2 Write a valid SQL query to retrieve

and display the SSN, last name, and classification of all math majors.

SELECT SSN, Lname, ClassFROM STUDENTWHERE Major = “Math”;

Page 8: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Practice 3 Write an SQL query to display the

SSN and last name of all seniors with a grade point average above 3.5.

Page 9: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Answer 3 Write an SQL query to display the

SSN and last name of all seniors with a grade point average above 3.5.

SELECT SSN, LnameFROM STUDENTWHERE Class = “Senior”

AND GPA > 3.5;

Page 10: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Practice 4 Write an SQL query to display the

name, department, title, and salary of all teachers who make between $30,000 and $40,000. Sort the results in ascending salary order.

Page 11: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Answer 4 Write an SQL query to display the

name, department, title, and salary of all teachers who make between $30,000 and $40,000. Sort the results in ascending salary order.

SELECT Name, Dept, Title, SalaryFROM TEACHERWHERE Salary between 30000 and

40000ORDER BY Salary;

Page 12: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Practice 5 Write an SQL query to count the

number of classes taught.

Page 13: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Answer 5 Write an SQL query to count the

number of classes taught.

SELECT Count(*)FROM CLASS; -OR-

SELECT Count(ClassNum)FROM CLASS;

Page 14: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Practice 6 Write an SQL query to display the

average GPA for each major. Sort the results by descending major.

Page 15: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Answer 6 Write an SQL query to display the

average GPA for each major. Sort the results by descending major.

SELECT Major, AVG(GPA)FROM STUDENTGROUP BY Major

ORDER BY Major Desc;

Page 16: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Practice 7 Write an SQL query to display the

faculty number, faculty name, and class numbers for all classes being taught.

Page 17: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Answer 7 Write an SQL query to display the

faculty number, faculty name, and class numbers for all classes being taught.

SELECT FacNum, Name, ClassNumFROM TEACHER, CLASSWHERE TEACHER.FacNum = CLASS.FacNum;

Page 18: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Practice 8 Write an SQL query to display the

class number, class name, teacher name, and credits for all classes with 1 credit hour. Sort by ascending class number.

Page 19: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Answer 8 Write an SQL query to display the

class number, class name, teacher name, and credits for all classes with 1 credit hour. Sort by ascending class number.

SELECT ClassNum, ClassName, Name, CreditsFROM TEACHER, CLASSWHERE TEACHER.FacNum = CLASS.FacNum

AND Credits = 1ORDER BY ClassNum;

Page 20: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Practice 9 Write an SQL query to display the

student number and last name, class name, and grade for all history majors.

Page 21: BACS 287 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below

BACS 485

SQL Answer 9 Write an SQL query to display the

student number and last name, class name, and grade for all history majors.

SELECT SSN, Lname, ClassName, GradeFROM STUDENT, ENROLL, CLASSWHERE STUDENT.SSN = ENROLL.SSN

AND CLASS.ClassNum = ENROLL.ClassNum AND Major = ‘History’;