Fundamentals of Database Systems
1
Database and Database Users
Module 9 Advanced SQL Query
Course Learning Outcomes:
1. Learn how to write SQL SELECT statement and know the result involving
Expression, Aggregate functions, Outer join, and Alias
Overview
More on SQL SELECT queries
• SQL Expression
• Aggregate functions with grouping
• More table join types and options
• Alias
Expression
It is a combination of symbols and operators that returns a single value.
An Expression can be:
• A single constant, column, variable, or scalar function
o 12
o 3.14
o ‘Anne Curtis’
o ‘05/18/16’
o CustomerName
o GetDate()
• A number, columns, literals, functions connected by operators
o 10*4+3
o ‘Anne’ + ‘Curtis’
o Address + ‘,’ + City + ‘,’ + Zipcode
o Quantity * Price
o 3.5 * Price
1.1 Expression used as Columns
Course Module
SELECT ProductName, Price * 2 AS ‘New Price’
FROM Products;
SELECT FirstName+ ‘ ‘ + LastName
FROM Students
SELECT GETDATE();
1.2 Expression used for Comparison
SELECT *
FROM Products
WHERE Stocks < 5;
SELECT ProductName, Price * 3 AS Discount
FROM Products
WHERE Price * 3 <= 15;
2. Aggregate Functions
Using aggregate functions for row calculation
• MAX – maximum of all or selected values
• MIN – minimum of all or selected values
• COUNT – number of all or selected values
• SUM – sum of all or selected values
• AVG – average of all or selected values
SELECT COUNT(ProductID) AS NumberOfProducts
FROM Products;
2.2 Grouping
GROUP BY – aggregation with groups. Used to get aggregation results for
different groups of records.
Fundamentals of Database Systems
3
Database and Database Users
SELECT CategoryID, AVG(Price)
FROM Products
GROUP BY CategoryID;
Limitations of GROUP BY
Columns or expressions can be in the SELECT clause only if they are in the GROUP BY clause.
3. Table Join Types
Inner Join
Records that have matching records from two (2) tables
Outer Join
▪ It is needed when minimum cardinality is optional on a table.
▪ Left Join – include all qualified records from the left table in the join condition even
if they do not have matching records in the right table.
▪ Right Join - include all qualified records from the right table in the join condition
even if they do not have matching records in the left table.
▪ Full Join – include all qualified records from both tables in the join condition.
Course Module
OUTER JOIN Example
Fundamentals of Database Systems
5
Database and Database Users
3. Alias
Course Module
References and Supplementary Materials
Books and Journals
1. Ramez Elmasri and Shamkant B. Navathe; 2016; Fundamentals of Database Systems;
USA; Pearson
2. Dr. Kashif Qureshi; 2018; Advanced concepts of information technology; educreation
publishing; India.
Online Supplementary Reading Materials
1. RelationalDBDesing;; March 31, 2020
2. Advantages of Database Management System;
https://www.tutorialspoint.com/Advantages-of-Database-Management-System;
March 31, 2020
3. DesigningandManagingData;
https://www.academia.edu/36712448/Entity_Relationship_Diagram_ERD_Basics_CIS
_3730_Designing_and_Managing_Data; April 01, 2020
4. DesigningandManagingData; http://jackzheng.net/teaching/archive/cis3730-2010-
fall/; April 03,2020
Online Instructional Videos
1. Introduction to Database; https://www.youtube.com/watch?v=8e-
wgQnsFxE&list=PLJ5C_6qdAvBHKccG0ZyOxcf_2YO6r4Q4l; March 21, 2020
2. Three levels of Architecture/DBMS;
https://www.youtube.com/watch?v=j6xh8wKfjkY; April 01,2020
3. Relational Data Model; https://www.youtube.com/watch?v=TsSf1Z3g0Kk; Arpil 06,
2020.
4. Basic Concept of Database Normalization;
https://www.youtube.com/watch?v=xoTyrdT9SZI; April 08, 2020
5. SQL Basics; https://www.youtube.com/watch?v=zbMHLJ0dY4w; April 10, 2020
6. Joins|SQL|; https://www.youtube.com/watch?v=efpFCd8iFAQ; April 11, 2020