SOEN 363: Data Systems for Software Engineers
Hamed Jafarpour
Fall 2025
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 1/63
Lecture information
Introduction to SQL
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 2/63
Outline
What is SQL?
SQL commands
In-class exercises
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 3/63
What is SQL?
SQL = Structured Query Language
Used to manage and manipulate relational databases
In SQL, DML (Data Manipulation Language) and DDL (Data
Definition Language) are two distinct categories of commands serving
different purposes within database managemen
Common operations:
Create databases and tables
Insert, update, delete data
Query data using SELECT
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 4/63
DDL - Data Definition Language
DDL statements define or modify the structure of the database.
Common DDL commands:
CREATE – create databases or tables
ALTER – modify table structure
DROP – delete tables or databases
TRUNCATE – remove all records from a table
Example: Creating a Table
1 CREATE TABLE Courses (
2 CourseID INT PRIMARY KEY ,
3 CourseName VARCHAR (100) ,
4 Credits INT
5 );
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 5/63
DML - Data Manipulation Language
DML statements are used to manipulate the data within tables.
Common DML commands:
INSERT – add new records
UPDATE – modify existing records
DELETE – remove records
SELECT – query and retrieve data
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 6/63
Creating a Database
Syntax:
1 CREATE DATABASE database_name ;
2 USE database_name ;
Note: USE database name in the above syntax, selects that database as
the “active” one, so any subsequent commands (like creating tables or
inserting data) will go into that database.
Example:
1 CREATE DATABASE SchoolDB ;
2 USE SchoolDB ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 7/63
Creating a Table
Syntax:
1 CREATE TABLE table_name (
2 column1 datatype constraints ,
3 column2 datatype constraints ,
4 ...
5 );
Example:
1 CREATE TABLE Students (
2 StudentID INT PRIMARY KEY ,
3 FirstName VARCHAR (50) ,
4 LastName VARCHAR (50) ,
5 Age INT ,
6 Major VARCHAR (50)
7 );
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 8/63
Inserting Data into Table
Syntax:
1 INSERT INTO table_name ( column1 , column2 , ...)
2 VALUES ( value1 , value2 , ...) ;
Example:
1 INSERT INTO Students ( StudentID , FirstName , LastName ,
Age , Major )
2 VALUES (1 , ’ Alice ’ , ’ Smith ’ , 20 , ’ Computer Science ’) ;
3
4 INSERT INTO Students ( StudentID , FirstName , LastName ,
Age , Major )
5 VALUES (2 , ’ Bob ’ , ’ Johnson ’ , 22 , ’ Mathematics ’) ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 9/63
SELECT command in SQL
Definition: The SELECT statement is used to retrieve data from one or
more tables in a database.
It can return all columns or specific columns, and can include conditions,
ordering, grouping, and more.
Syntax:
1 SELECT column1 , column2 , ...
2 FROM table_name ;
Example:
1 -- Select FirstName , LastName , Age from all students
2 SELECT FirstName , LastName , Age
3 FROM Students
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 10/63
SELECT with WHERE
Definition: The WHERE clause filters rows based on conditions.
Syntax:
1 SELECT column1 , column2 , ...
2 FROM table_name
3 WHERE condition ;
Example:
1 -- Select students older than 20
2 SELECT FirstName , LastName , Age
3 FROM Students
4 WHERE Age > 20;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 11/63
Removing Duplicates with DISTINCT
Definition: The DISTINCT keyword is used in SQL to return only unique
values, eliminating duplicates from the result set.
Syntax:
1 SELECT DISTINCT column1 , column2 , ...
2 FROM table_name ;
Examples:
1 -- List unique majors ( no duplicates )
2 SELECT DISTINCT Major
3 FROM Students ;
4
5 -- List unique combinations of first and last names
6 SELECT DISTINCT FirstName , LastName
7 FROM Students ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 12/63
WHERE with AND and OR
Definition: - AND: returns rows where all conditions are true.
- OR: returns rows where at least one condition is true.
Syntax:
1 SELECT column1 , column2 , ...
2 FROM table_name
3 WHERE condition1 AND condition2 ;
4
5 SELECT column1 , column2 , ...
6 FROM table_name
7 WHERE condition1 OR condition2 ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 13/63
Examples: WHERE with AND and OR
Examples:
1 -- Students older than 20 AND majoring in CS
2 SELECT FirstName , LastName , Age , Major
3 FROM Students
4 WHERE Age > 20 AND Major = ’ Computer Science ’;
5
6 -- Students majoring in Math OR older than 22
7 SELECT FirstName , LastName , Age , Major
8 FROM Students
9 WHERE Major = ’ Mathematics ’ OR Age > 22;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 14/63
Combining AND and OR
Definition: You can combine AND and OR together. Use parentheses to
control evaluation order.
Syntax:
1 SELECT column1 , column2 , ...
2 FROM table_name
3 WHERE ( condition1 OR condition2 ) AND condition3 ;
Example:
1 -- Students majoring in CS or Math AND older than 20
2 SELECT FirstName , LastName , Age , Major
3 FROM Students
4 WHERE ( Major = ’ Computer Science ’ OR Major = ’
Mathematics ’)
5 AND Age > 20;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 15/63
SELECT with ORDER BY
Definition: The ORDER BY clause sorts rows in ascending (ASC) or
descending (DESC) order.
Syntax:
1 SELECT column1 , column2 , ...
2 FROM table_name
3 ORDER BY column1 [ ASC | DESC ];
Example:
1 -- List students ordered by Age ( youngest first )
2 SELECT FirstName , LastName , Age
3 FROM Students
4 ORDER BY Age ASC ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 16/63
SELECT with GROUP BY
Definition: The GROUP BY clause groups rows that have the same values
and allows aggregation functions like COUNT, AVG, SUM.
It is always used with aggregate functions (like COUNT, SUM, AVG, MIN,
MAX) to produce summarized results. Syntax:
1 SELECT column1 , AGG_FUNCTION ( column2 )
2 FROM table_name
3 GROUP BY column1 ;
Example:
1 -- Count students in each major
2 SELECT Major , COUNT (*) AS TotalStudents
3 FROM Students
4 GROUP BY Major ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 17/63
SELECT with HAVING
Definition: The HAVING clause filters groups after aggregation (like
WHERE, but for grouped data).
Syntax:
1 SELECT column1 , AGG_FUNCTION ( column2 )
2 FROM table_name
3 GROUP BY column1
4 HAVING condition ;
Example:
1 -- Show majors with more than 2 students
2 SELECT Major , COUNT (*) AS TotalStudents
3 FROM Students
4 GROUP BY Major
5 HAVING COUNT (*) > 2;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 18/63
Pattern Matching with LIKE
Definition: The LIKE operator is used in SQL to search for a specified
pattern in a column.
-% matches any sequence of characters (including empty).
- matches exactly one character.
Syntax:
1 SELECT column1 , column2 , ...
2 FROM table_name
3 WHERE column_name LIKE pattern ;
Examples:
1 -- Names starting with ’A ’
2 SELECT FirstName FROM Students
3 WHERE FirstName LIKE ’A % ’;
4
5 -- Names ending with ’son ’
6 SELECT LastName FROM Students
7 WHERE LastName LIKE ’% son ’;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 19/63
UNION in SQL
Definition: The UNION operator combines the result sets of two queries
and removes duplicates by default.
Syntax:
1 SELECT column_list
2 FROM table1
3 UNION
4 SELECT column_list
5 FROM table2 ;
Example:
1 -- Combine all student majors from two tables
2 SELECT Major FROM Students
3 UNION
4 SELECT Major FROM Alumni ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 20/63
INTERSECT in SQL
Definition: The INTERSECT operator returns only the rows that are
common to the result sets of both queries.
Syntax:
1 SELECT column_list
2 FROM table1
3 INTERSECT
4 SELECT column_list
5 FROM table2 ;
Example:
1 -- Find majors that exist in both Students and Alumni
2 SELECT Major FROM Students
3 INTERSECT
4 SELECT Major FROM Alumni ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 21/63
EXCEPT in SQL
Definition: The EXCEPT operator returns rows from the first query that
are not present in the second query.
Syntax:
1 SELECT column_list
2 FROM table1
3 EXCEPT
4 SELECT column_list
5 FROM table2 ;
Example:
1 -- Find majors present in Students but not in Alumni
2 SELECT Major FROM Students
3 EXCEPT
4 SELECT Major FROM Alumni ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 22/63
In class exercise: Sample Students Table
ID First Name Last Name Age Major Year
1 Alice Smith 20 Computer Science Sophomore
2 Bob Johnson 22 Mathematics Senior
3 Carol Brown 19 Physics Freshman
4 David Miller 21 Biology Junior
5 Emma Davis 23 Chemistry Senior
6 Frank Wilson 20 Engineering Sophomore
7 Grace Taylor 18 Economics Freshman
8 Henry Anderson 24 Philosophy Graduate
9 Irene Thomas 21 Psychology Junior
10 Jack White 22 Literature Senior
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 23/63
Q1: Select all first names of students
Question: Select all first names of students from the table.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 24/63
Answer Q1
1 SELECT FirstName
2 FROM Students ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 25/63
Q2: Select students majoring in ’Mathematics’
Question: Select all students whose Major is ’Mathematics’.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 26/63
Answer Q2
1 SELECT *
2 FROM Students
3 WHERE Major = ’ Mathematics ’;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 27/63
Q3: Select students older than 21
Question: Select the first name, last name, and age of students older
than 21.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 28/63
Answer Q3
1 SELECT FirstName , LastName , Age
2 FROM Students
3 WHERE Age > 21;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 29/63
Q4: Select unique majors
Question: Select all unique majors from the Students table.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 30/63
Answer Q4
1 SELECT DISTINCT Major
2 FROM Students ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 31/63
Q5: Count students in each year
Question: Count how many students are in each Year.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 32/63
Answer Q5
1 SELECT Year , COUNT (*) AS TotalStudents
2 FROM Students
3 GROUP BY Year ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 33/63
Q6: Years with more than 2 students
Question: Show the years that have more than 2 students.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 34/63
Answer Q6
1 SELECT Year , COUNT (*) AS TotalStudents
2 FROM Students
3 GROUP BY Year
4 HAVING COUNT (*) > 2;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 35/63
Q7: Students majoring in CS or Engineering
Question: Select students whose Major is ’Computer Science’ or
’Engineering’.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 36/63
Answer Q7
1 SELECT *
2 FROM Students
3 WHERE Major = ’ Computer Science ’
4 OR Major = ’ Engineering ’;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 37/63
Q8: Combine Physics and Chemistry students using
UNOIN command
Question: Combine two queries to get all students majoring in ’Physics’
or ’Chemistry’.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 38/63
Answer Q8
1 SELECT * FROM Students
2 WHERE Major = ’ Physics ’
3 UNION
4 SELECT * FROM Students
5 WHERE Major = ’ Chemistry ’;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 39/63
Q9: Majors common to students older than 20 and Seniors
Question: Find majors that exist both for students older than 20 and
students in Senior year.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 40/63
Answer Q9
1 SELECT Major FROM Students
2 WHERE Age > 20
3 INTERSECT
4 SELECT Major FROM Students
5 WHERE Year = ’ Senior ’;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 41/63
Q10: Students who are not Seniors
Question: Find all students who are not in Senior year.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 42/63
Answer Q10
1 SELECT * FROM Students
2 EXCEPT
3 SELECT * FROM Students
4 WHERE Year = ’ Senior ’;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 43/63
Q11: Students majoring in CS or Engineering AND
younger than 21
Question: Select students whose Major is ’Computer Science’ or
’Engineering’ AND Age less than 21.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 44/63
Answer Q11
1 SELECT *
2 FROM Students
3 WHERE ( Major = ’ Computer Science ’ OR Major = ’
Engineering ’)
4 AND Age < 21;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 45/63
Q12: List all Seniors sorted by Age descending
Question: Select all Seniors and sort them by Age in descending order.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 46/63
Answer Q12
1 SELECT FirstName , LastName , Age , Year
2 FROM Students
3 WHERE Year = ’ Senior ’
4 ORDER BY Age DESC ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 47/63
Q13: Count students in each Major older than 20
Question: Count the number of students in each Major who are older
than 20.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 48/63
Answer Q13
1 SELECT Major , COUNT (*) AS TotalStudents
2 FROM Students
3 WHERE Age > 20
4 GROUP BY Major ;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 49/63
Q14: Majors with more than 1 student and average age
above 21
Question: Find Majors that have more than 1 student and average age
above 21.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 50/63
Answer Q14
1 SELECT Major , COUNT (*) AS TotalStudents , AVG ( Age ) AS
AvgAge
2 FROM Students
3 GROUP BY Major
4 HAVING COUNT (*) > 1 AND AVG ( Age ) > 21;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 51/63
Q15: Students whose first name starts with ’A’ or ’E’
Question: Select all students whose first name starts with ’A’ or ’E’.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 52/63
Answer Q15
1 SELECT *
2 FROM Students
3 WHERE FirstName LIKE ’A % ’ OR FirstName LIKE ’E % ’;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 53/63
Q16: Students with Age between 20 and 23 and not
Seniors
Question: Select students aged between 20 and 23 who are not Seniors.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 54/63
Answer Q16
1 SELECT *
2 FROM Students
3 WHERE Age BETWEEN 20 AND 23
4 AND Year <> ’ Senior ’;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 55/63
Q17: Majors in Students but not in Seniors
Question: Find Majors present in the table but not in Senior students.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 56/63
Answer Q17
1 SELECT Major FROM Students
2 EXCEPT
3 SELECT Major FROM Students
4 WHERE Year = ’ Senior ’;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 57/63
Q18: Majors of Freshman or Graduate students with Age ¡
22
Question: Find all distinct Majors of students who are either Freshman or
Graduate and age ¡ 22.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 58/63
Answer Q18
1 SELECT DISTINCT Major
2 FROM Students
3 WHERE ( Year = ’ Freshman ’ OR Year = ’ Graduate ’)
4 AND Age < 22;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 59/63
Q19: Majors common to Sophomore and Junior students
Question: Find Majors that appear both in Sophomore and Junior
students.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 60/63
Answer Q19
1 SELECT Major FROM Students
2 WHERE Year = ’ Sophomore ’
3 INTERSECT
4 SELECT Major FROM Students
5 WHERE Year = ’ Junior ’;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 61/63
Q20: Students older than 21 or majoring in Physics
Question: Combine students older than 21 with students majoring in
Physics.
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 62/63
Answer Q20
1 SELECT * FROM Students
2 WHERE Age > 21
3 UNION
4 SELECT * FROM Students
5 WHERE Major = ’ Physics ’;
Hamed Jafarpour Department of Computer Science and Software Engineering Concordia University 63/63