Fundamentals of Database Systems
1
Database and Database Users
Module 7 SQL Basics
Course Learning Outcomes:
1. Understand what SQL is
2. Learn what does SQL do
3. Learn how to write SQL SELECT statement to retrieve data
Overview
Structured Query Language or also known as SQL is used to retrieve data from
relational databases. We will learn the basic syntax and simple queries.
SQL
Structured Query Language is a command use by relational database management
system to perform database operations.
SQL Facts
• The command language used is like an English language.
• It is NOT Case sensitive
• It’s not a Programming Language
What does SQL do
DML – Data Manipulation Language
• Used to retrive data and manipulate data
• Retrieve data using database queries
• Manipulate data using commands: insert, update and delete
Course Module
DDL – Data Definition Language
• Used to define and modify data structures
• Schema: databases, tables, views and many more
DCL – Data Control Language
• Used to control data access
• Use the command: Permissions, etc.
Database Queries
Query – is a command used or a request for information from a database.
SELECT statements - used to retrieve data from database.
SQL SELECT basics
• Defining selection criteria
• Sorting
• Table join
General Syntax of SELECT:
SELECT [ Column(s), or other expression]
FROM [Table(s)]
[WHERE …]
[ORDER BY …]
1. SELECT Columns
Syntax
SELECT *
FROM Tablename
“NOTE: asterisk (*) means all or a list of columns”
Fundamentals of Database Systems
3
Database and Database Users
Select Columns Example
SELECT *
FROM Products;
SELECT ProductName, ProductPrice
FROM Products;
Column Qualifier and Alias
Use qualifiers if there is ambiguity when distinguishing a column.
SELECT Products.ProductName, Products.ProductPrice
FROM Products;
Use ALIAS to give column another name
SELECT ProductPrice * Quantity AS Total
FROM Order Details;
NOTE: Use the word “AS” for aliasing.
2. SELECT Rows
We use the word “WHERE” to specify selection criteria
We also need to use the different comparison operators
• = (Equal)
• > (Greater than)
• < (Less than)
• >= (Greater than or equal to)
• <= (Less than or equal to)
• ! = (Not equal)
Course Module
Select Rows Examples
SELECT *
FROM Products
WHERE Price = 18;
SELECT BookTitle, ListPrice
FROM Products
WHERE Price < 20;
Comparing with Constants
2.2 IN and BETWEEN
IN: Equals to any value in the list
SELECT *
FROM Products
WHERE Price IN (11, 12, 15);
Fundamentals of Database Systems
5
Database and Database Users
BETWEEN min AND max: any value falls in the range, inclusive
SELECT *
FROM Products
WHERE Price BETWEEN 15 and 20;
2.3 String Pattern Match: LIKE
LIKE: Keyword Match
• _ (underscore): use to match one single character
• % (percentage) use to match any multiple characters
SELECT *
FROM Products
WHERE ProductName LIKE ‘%tofu%;
2.4 IS NULL
NULL means missing value
SELECT *
FROM Orders
WHERE PostalCode IS NULL;
SELECT *
FROM Orders
WHERE PostalCode IS NOT NULL;
Course Module
2.5 NOT
Reversal Criteria
Use the word “NOT”
SELECT *
FROM Products
WHERE NOT Price > 20.
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; https://www.relationaldbdesign.com/basic-sql/module3/intro-
relational-databases.php; 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
Fundamentals of Database Systems
7
Database and Database Users
Course Module