Lecture 4
Lecture 4
5 credits)
Yuantao Fan
[email protected]
Halmstad University
Overview
• Ordering
• Aliansing
• Aggregation
• Grouping
• Having
• Lab1 Introduction
Teacher(tid, full_name, age, nationality)
Example Database tid full_name age nationality
• Output control
• Ordering the output tuples by the values in one for more columns
• Syntax
– ORDER BY <column> [ASC/DESC]
• Output control
• Ordering the output tuples by the values in one for more columns
• Syntax
– ORDER BY <column> [ASC/DESC]
• Output control
• Ordering the output tuples by the values in one for more columns
• Syntax
– ORDER BY <column> [ASC/DESC]
• Aggregate function are built-in and can be applied in the SELECT output list
• Aggregate function are almost always used in the output from SELECT statement
• Aggregate function are almost always used in the output from SELECT statement
• Aggregate function are almost always used in the output from SELECT statement
• Aggregate function are almost always used in the output from SELECT statement
• Aggregate function are almost always used in the output from SELECT statement
SELECT AVG(age)
FROM Teacher
WHERE nationality='SWEDEN';
Aggregation with multiple columns
• Acquire the number of teachers and their average age that come from Sweden
Aggregation with multiple columns
• Acquire the number of teachers and their average age that come from Sweden
• DISTINCT
– Acquire distinctive values from column(s)
– Operation set
• DISTINCT
– Acquire distinctive values from column(s)
– Operation set
SELECT DISTINCT(nationality)
FROM Teacher
Aggregation
• DISTINCT
– Acquire distinctive values from column(s)
– Operation set
• DISTINCT
– Acquire distinctive values from column(s)
– Operation set
SELECT COUNT(DISTINCT(nationality))
FROM Teacher
Aggregation
• Note that output of other columns excluded from the aggregation is undefined
• Note that output of other columns excluded from the aggregation is undefined
• Note that output of other columns excluded from the aggregation is undefined
• Note that output of other columns excluded from the aggregation is undefined
• Requires group by
• Note that output of other columns excluded from the aggregation is undefined
• Requires group by
• Note that output of other columns excluded from the aggregation is undefined
• Note that output of other columns excluded from the aggregation is undefined
• Note that output of other columns excluded from the aggregation is undefined
• Note that output of other columns excluded from the aggregation is undefined
• HAVING
– Filter based on aggregated results
– Consider it a WHERE clause for GROUP BY
• Content
– Create a relational database with multiple tables
– writting queries given a question/description
• Practising basic SQL statement
– Propose queries
– Get familiar with a real world database - imdb
Lab 1 Introduction
e3 Tim EE 20 3.9 c1 33 A
Course m4 11 A
• Queries
– Select all students above the age of 20
– Who is the oldest student?
– Count the number of students with age below 20
– How many types of majors were these students admitted to?
– What is the average GPA of students with age above 20?
– What is the average GPA of students studying the Physics major?
– What is the average age of students who took Linear algebra courses?
– How many courses has Alice registered for?
– How many credits has Alice registered?
– How many credits have students with age below 20 registered to?
• sql_inventory
– What is the most valuable asset in the inventory?
– How much does the entire inventory worth?
• sql_hr
– Where is the largest office (in terms of numbers of employees) located?
– Who sits alone?
• sql_invoicing
– What is the most common payment method?
– Which client seems to be the most important one? Motivate your approach and answer.
• sql_store
– How much do order 2 worth?
– Which customer has their order delivered?
Lab 1.3 Tasks
• Tool
– SQLite3 – make sure you have access to it
– https://www.sqlite.org/download.html
• Movie database
– Download the zip file of the database from the blackboard
• Queries
– How many movies have the highest rating?
– What are the most common genres in this database?
– Which movie is the longest?