SQL Query Techniques

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which of the following queries will list all details from the staff table, placing individuals with the last name 'Yandle' at the top of the result set?

  • `SELECT * FROM staff WHERE lastname = 'Yandle';`
  • `SELECT * FROM staff ORDER BY lastname DESC;`
  • `SELECT * FROM staff ORDER BY firstname ASC;`
  • `SELECT * FROM staff ORDER BY lastname ASC;` (correct)

Consider a database schema with a course table. Which SQL query accurately selects the name of all courses that are either level 3 or 4, have more than 10 credits, and are not offered in semester 2?

  • `SELECT name FROM course WHERE semester = 2 AND credits > 10;`
  • `SELECT name FROM course WHERE credits > 10;`
  • `SELECT name FROM course WHERE level > 2 AND credits > 10;`
  • `SELECT name FROM course WHERE level IN (3, 4) AND credits > 10 AND semester != 2;` (correct)

Given a staff table, which SQL query effectively identifies staff members who have either a missing phone number or a missing office number in the database?

  • `SELECT lastname FROM staff WHERE phone IS NULL AND office IS NULL;`
  • `SELECT lastname FROM staff WHERE phone IS NULL OR office IS NULL;` (correct)
  • `SELECT lastname FROM staff WHERE phone IS NOT NULL AND office IS NOT NULL;`
  • `SELECT lastname FROM staff WHERE phone IS NOT NULL OR office IS NOT NULL;`

Assuming tables named allmarks17 and course exist, which SQL query correctly retrieves the banner code (bc) and name of courses taken by students in the year 2017?

<p><code>SELECT bc, name FROM allmarks17, course WHERE allmarks17.bc = course.bc;</code> (D)</p> Signup and view all the answers

Using SQL, how would you list the name of courses that were not taught in 2017 but had students who took exams for them in 2017, assuming data exists in tables named course, allmarks17, and lecturing?

<p><code>SELECT name FROM course WHERE bc IN (SELECT bc FROM allmarks17) AND cid NOT IN (SELECT cid FROM lecturing WHERE year = 2017);</code> (C)</p> Signup and view all the answers

Considering a staff table with phone and office columns, which SQL query identifies the last names of staff who have a phone number recorded but do not have an office number recorded, utilizing the EXCEPT operator?

<p><code>SELECT lastname FROM staff WHERE phone IS NOT NULL EXCEPT SELECT lastname FROM staff WHERE office IS NOT NULL;</code> (D)</p> Signup and view all the answers

Given a lecturing table, which SQL query lists course IDs (cid) for courses that were newly introduced in the year 2015, meaning they were not taught in any year prior to 2015?

<p><code>SELECT cid FROM lecturing WHERE year = 2015 AND cid NOT IN (SELECT cid FROM lecturing WHERE year &lt; 2015);</code> (D)</p> Signup and view all the answers

Assuming a lecturing table exists, which SQL query correctly lists course IDs (cid) along with the average enrolment (AVG(numbers)) and the count of distinct staff members (COUNT(DISTINCT sid)) for each course?

<p><code>SELECT cid, AVG(numbers), COUNT(DISTINCT sid) FROM lecturing GROUP BY cid;</code> (B)</p> Signup and view all the answers

Given a lecturing table, which SQL query identifies courses that were repeated in the same year, using the GROUP BY clause?

<p><code>SELECT cid, year FROM lecturing GROUP BY cid, year HAVING COUNT(*) &gt; 1;</code> (B)</p> Signup and view all the answers

With a table named allmarks17, which SQL query computes the total number of distinct students and courses in a single query?

<p><code>SELECT COUNT(DISTINCT student), COUNT(DISTINCT bc) FROM allmarks17;</code> (B)</p> Signup and view all the answers

Assuming there is a table named allmarks, which SQL query lists course banner codes (bc) along with the average marks for both 2016 and 2017, but only for courses that have data for both years?

<p><code>SELECT bc, AVG(CASE WHEN year = 2016 THEN mark END), AVG(CASE WHEN year = 2017 THEN mark END) FROM allmarks WHERE year IN (2016, 2017) GROUP BY bc HAVING COUNT(DISTINCT year) = 2;</code> (A)</p> Signup and view all the answers

Given tables named staff and lecturing, which SQL query lists the last names of staff members who did not lecture?

<p><code>SELECT lastname FROM staff WHERE sid NOT IN (SELECT sid FROM lecturing);</code> (A)</p> Signup and view all the answers

How can you generate a course statistics report ordered alphabetically by course name, given tables named course and allmarks18?

<p><code>SELECT name, COUNT(student), AVG(mark), MAX(mark), MIN(mark), STDDEV(mark) FROM course, allmarks18 WHERE course.bc = allmarks18.bc GROUP BY name ORDER BY name;</code> (B)</p> Signup and view all the answers

In a database with a staff table, how would you identify staff members who share an office?

<p><code>SELECT s1.lastname FROM staff s1, staff s2 WHERE s1.office = s2.office AND s1.sid != s2.sid;</code> (A)</p> Signup and view all the answers

Given the relational algebra expression σ_{D=p}(π_{X,Y}(T1) ⋈ π_{X,D}(T2)), what is the number of tuples in the result?

<p>2 tuples (A)</p> Signup and view all the answers

Which relational algebra expression is equivalent to the following SQL query: Find staff names with lastname 'Jung' from the years 2013 or 2014?

<p>π_{name}(σ_{lastname='Jung' ∧ (year=2013 ∨ year=2014)}(staff ⋈ lecturing ⋈ course)) (C)</p> Signup and view all the answers

Assuming a database contains a lecturing table, which SQL query lists the course IDs (cid) of courses that had an increase in enrolment numbers from 2016 to 2017?

<p><code>SELECT l1.cid FROM lecturing l1, lecturing l2 WHERE l1.cid = l2.cid AND l1.year = 2016 AND l2.year = 2017 AND l1.numbers &lt; l2.numbers;</code> (A)</p> Signup and view all the answers

Given a table named allmarks18, which SQL query calculates the 'hard-failure rate' for each course?

<p><code>SELECT bc, COUNT(CASE WHEN mark &lt; 40 THEN 1 END) / COUNT(*)::FLOAT FROM allmarks18 GROUP BY bc;</code> (A)</p> Signup and view all the answers

What information is returned by the following relational algebra expression: π_{lastname}(σ_{numbers > 100}(staff ⋈ lecturing))?

<p>Last names of staff teaching courses with over 100 students (C)</p> Signup and view all the answers

Which SQL query accurately represents the following relational algebra expression: π_{name}(σ_{numbers > 100}(lecturing ⋈ course)) - π_{name}(σ_{level=1}(course))?

<p><code>SELECT name FROM lecturing, course WHERE lecturing.cid = course.cid AND numbers &gt; 100 EXCEPT SELECT name FROM course WHERE level = 1;</code> (B)</p> Signup and view all the answers

Given tables T1(A, B, C) and T2(B, C, D), how many tuples result from the natural inner join T1 ⋈ T2?

<p>2 (A)</p> Signup and view all the answers

Given tables T1(A, B, C) and T2(B, C, D), how many tuples result from the natural full outer join T1 ⟗⟖ T2?

<p>7 (B)</p> Signup and view all the answers

In a tourist information system, what is the most likely multiplicity relationship between City and County?

<p>(1,1) from City to County, (0,many) from County to City (B)</p> Signup and view all the answers

For a hotel database, which description best defines the relationship of a Room to Accommodation?

<p>Weak entity with (1,many) to Accommodation (B)</p> Signup and view all the answers

In a car rental system, what is the multiplicity relationship between Customer and Vehicle Class?

<p>(0,many) both ways (D)</p> Signup and view all the answers

Which of the following schemas correctly represents the Attraction table in a tourist information database?

<p>Attraction(attraction_id, name, city_id°, street_id) (C)</p> Signup and view all the answers

Which SQL CREATE TABLE statement correctly corresponds to a Street table, including a foreign key relationship to a City table with cascade delete?

<p>CREATE TABLE Street (street_id INT PRIMARY KEY, name VARCHAR(50), length INT, city_id INT REFERENCES City(city_id) ON DELETE CASCADE); (C)</p> Signup and view all the answers

In a hotel context, what multiplicity best describes the relationship between Hotel and Meeting Room?

<p>(0,many) from Hotel to Meeting Room, (1,1) from Meeting Room to Hotel (C)</p> Signup and view all the answers

Within the context of the UN system, which multiplicity best describes the relationship between a Country and an Alliance?

<p>(0,many) both ways (B)</p> Signup and view all the answers

In a car rental system, how should the eligibility of employees to provide discounts to commercial customers be modeled?

<p>As a relationship between Commercial Customer and Employee (D)</p> Signup and view all the answers

Which database schema best represents the Rental table in a car rental system?

<p>Rental(rental_id, customer_id, vehicle_id, class, start_date, end_date) (C)</p> Signup and view all the answers

In a car rental system, how would you represent the relationship between commercial customers and their eligible employees?

<p>Eligible(customer_id, employee_id) (D)</p> Signup and view all the answers

Flashcards

ORDER BY lastname ASC

Sorts results alphabetically, so 'Yandle' appears early.

Course Selection Query

Filters by level (3 or 4), credits (>10), excluding semester 2.

IS NULL Operator

Checks for staff with either missing phone OR office numbers.

Table JOIN

Combines tables to find courses taken by students in 2017.

Signup and view all the flashcards

IN and NOT IN

Finds courses with exams in 2017 but not taught that year.

Signup and view all the flashcards

EXCEPT Operator

Lists staff with a phone, removing those with office numbers.

Signup and view all the flashcards

Find New Courses

Lists courses new in 2015 (not taught before).

Signup and view all the flashcards

AVG and COUNT(DISTINCT)

Calculates average enrolment and unique staff count per course.

Signup and view all the flashcards

GROUP BY for Duplicates

Finds courses repeated in the same year using GROUP BY.

Signup and view all the flashcards

COUNT(DISTINCT)

Counts unique students and courses in the allmarks17 table.

Signup and view all the flashcards

CASE WHEN with AVG

Lists courses with average marks for both 2016 and 2017.

Signup and view all the flashcards

Staff Who Didn't Lecture

Lists staff who have not lectured any courses.

Signup and view all the flashcards

Aggregate Course Stats

Shows course statistics ordered alphabetically by course name.

Signup and view all the flashcards

Self-Join

Finds staff members who share the same office.

Signup and view all the flashcards

Join with Filtering

Last names of staff teaching courses with over 100 students.

Signup and view all the flashcards

City-County relationship

Multiplicity in tourist info system

Signup and view all the flashcards

Hotels and Room Relationship

Room depend on what?

Signup and view all the flashcards

Customer and Vehicle Classes relation

Car rental system multiplicity

Signup and view all the flashcards

Attraction Table

The correct Attraction table schema

Signup and view all the flashcards

Street Table

Create a street table

Signup and view all the flashcards

Hotel and Meeting Room relation

What multiplicity between Hotel and Meeting Room?

Signup and view all the flashcards

Country and Alliance Multiplicity

Multiplicity between Country and Alliance

Signup and view all the flashcards

Employe eligibility modeled

Employees can rent rentals

Signup and view all the flashcards

Rental attributes

Tracks specific rentals with foreign keys to customer and vehicle.

Signup and view all the flashcards

Study Notes

Quiz 1: SQL Focus

  • SELECT * FROM staff ORDER BY lastname ASC lists staff details with those having last names alphabetically at the beginning of the list.
  • SELECT name FROM course WHERE level IN (3, 4) AND credits > 10 AND semester != 2 finds courses at level 3 or 4 with more than 10 credits, excluding semester 2.
  • SELECT lastname FROM staff WHERE phone IS NULL OR office IS NULL identifies staff members who have a missing phone number or office number.
  • SELECT bc, name FROM allmarks17, course WHERE allmarks17.bc = course.bc finds courses taken by students in 2017 by joining the allmarks17 table with the course table.
  • SELECT name FROM course WHERE bc IN (SELECT bc FROM allmarks17) AND cid NOT IN (SELECT cid FROM lecturing WHERE year = 2017) lists courses with exams taken in 2017 but not taught in 2017.
  • SELECT lastname FROM staff WHERE phone IS NOT NULL EXCEPT SELECT lastname FROM staff WHERE office IS NOT NULL finds staff with a phone number but no office number.
  • SELECT cid FROM lecturing WHERE year = 2015 AND cid NOT IN (SELECT cid FROM lecturing WHERE year < 2015) lists courses new in 2015 that were not taught earlier.
  • SELECT cid, AVG(numbers), COUNT(DISTINCT sid) FROM lecturing GROUP BY cid lists average enrollment numbers and distinct staff count for courses
  • SELECT cid, year FROM lecturing GROUP BY cid, year HAVING COUNT(*) > 1 finds courses repeated in the same year.
  • SELECT COUNT(DISTINCT student), COUNT(DISTINCT bc) FROM allmarks17 computes the number of distinct students and courses in allmarks17.
  • SELECT bc, AVG(CASE WHEN year = 2016 THEN mark END), AVG(CASE WHEN year = 2017 THEN mark END) FROM allmarks WHERE year IN (2016, 2017) GROUP BY bc HAVING COUNT(DISTINCT year) = 2 lists courses with average marks for both 2016 and 2017.
  • SELECT lastname FROM staff WHERE sid NOT IN (SELECT sid FROM lecturing) lists staff who did not lecture.

Quiz 2: SQL & Relational Algebra Focus

  • SELECT name, COUNT(student), AVG(mark), MAX(mark), MIN(mark), STDDEV(mark) FROM course, allmarks18 WHERE course.bc = allmarks18.bc GROUP BY name ORDER BY name shows course statistics (count of students, average mark, max mark, min mark, standard deviation of mark) ordered by name.
  • SELECT s1.lastname FROM staff s1, staff s2 WHERE s1.office = s2.office AND s1.sid != s2.sid finds staff members who share an office, using a self join to compare staff records.
  • The relational algebra expression σ_{D=p}(Ï€_{X,Y}(T1) ⋈ Ï€_{X,D}(T2)) results in 2 tuples, after projecting, joining, and filtering.
  • Ï€_{name}(σ_{lastname='Jung' ∧ (year=2013 ∨ year=2014)}(staff ⋈ lecturing ⋈ course)) relational algebra expression retrieves the names of staff members with the last name 'Jung' who lectured in either 2013 or 2014 and joins staff, lecturing, and course tables.
  • SELECT l1.cid FROM lecturing l1, lecturing l2 WHERE l1.cid = l2.cid AND l1.year = 2016 AND l2.year = 2017 AND l1.numbers < l2.numbers lists the courses with increased enrollment from 2016 to 2017 by comparing the number of students enrolled in the same course in those years.
  • SELECT bc, COUNT(CASE WHEN mark < 40 THEN 1 END) / COUNT(*)::FLOAT FROM allmarks18 GROUP BY bc calculates the hard-failure rate for courses in allmarks18.
  • Ï€_{lastname}(σ_{numbers > 100}(staff ⋈ lecturing)) returns the last names of staff teaching courses with over 100 students through joining the staff and lecturing tables.
  • SELECT name FROM lecturing, course WHERE lecturing.cid = course.cid AND numbers > 100 EXCEPT SELECT name FROM course WHERE level = 1 SQL query matches the relational algebra expression Ï€_{name}(σ_{numbers > 100}(lecturing ⋈ course)) - Ï€_{name}(σ_{level=1}(course)).
  • The natural inner join of tables T1 and T2 results in 2 tuples.
  • The natural full outer join of tables T1 and T2 results in 7 tuples.

Quiz 3: ER Modeling & Logical Design Focus

  • In a tourist information system, the multiplicity between City and County is (1,1) from City to County, and (0,many) from County to City.
  • For a hotel database, the Room relationship to Accommodation is best described as a weak entity with a (1,many) relationship to Accommodation.
  • In a car rental system, the multiplicity between Customer and Vehicle Class is (0,many) both ways.
  • The schema that correctly represents the Attraction table is Attraction(attraction_id, name, city_id°, street_id).
  • CREATE TABLE Street (street_id INT PRIMARY KEY, name VARCHAR(50), length INT, city_id INT REFERENCES City(city_id) ON DELETE CASCADE) is the SQL CREATE statement thatmatches the Street table.
  • In the hotel system, the multiplicity between Hotel and Meeting Room is (0,many) from Hotel to Meeting Room, and (1,1) from Meeting Room to Hotel.
  • In the UN system, the multiplicity between Country and Alliance is (0,many) both ways.
  • In the car rental system, Employee eligibility is modeled as a relationship between Commercial Customer and Employee.
  • The Rental table is represented by the schema Rental(rental_id, customer_id, vehicle_id, class, start_date, end_date).
  • The Commercial Customer’s eligible Employees is represented by the schema Eligible(customer_id, employee_id).

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

Database Quiz: Company Contacts
5 questions
[05/Sanggan/06]
15 questions

[05/Sanggan/06]

InestimableRhodolite avatar
InestimableRhodolite
SQL Queries for Worker Table
6 questions

SQL Queries for Worker Table

CommendableFourier1867 avatar
CommendableFourier1867
Use Quizgecko on...
Browser
Browser