You are on page 1of 98

Introduction

Structured Query Language


(SQL)
Course Level : Beginners (University Students)
Instructor : Ahmad H. Al Kheraisat
Information Technology Center
Director Technical Assistant
What is SQL ?

What is The Purpose of SQL

03/23/2024 2
The Most popular database
management system (DBMS)

03/23/2024 3
Microsoft SQL
Server 2019
Express
Installation link
https://www.microsoft.com/en-us/Download/details.aspx?id=101064
SQL Server 2019 Express Installation

03/23/2024 5
SQL Server 2019 Express Installation

03/23/2024 PRESENTATION TITLE 6


SQL Server 2019 Express Installation

03/23/2024 PRESENTATION TITLE 7


SQL Server 2019 Express Installation

03/23/2024 8
SQL Server 2019 Express Installation

03/23/2024 PRESENTATION TITLE 9


SQL Server 2019 Express Installation

03/23/2024 PRESENTATION TITLE 10


SQL Server 2019 Express Installation

03/23/2024 PRESENTATION TITLE 11


SQL Server 2019 Express Installation

03/23/2024 PRESENTATION TITLE 12


SQL Server 2019 Express Installation

03/23/2024 PRESENTATION TITLE 13


SQL Server 2019 Express Installation

03/23/2024 PRESENTATION TITLE 14


SQL Server 2019 Express Installation

03/23/2024 15
SQL Server Management Studio (SSMS)

03/23/2024 16
SQL Server Management Studio Installation

03/23/2024 PRESENTATION TITLE 17


SQL Server Management Studio Installation

03/23/2024 PRESENTATION TITLE 18


Starting SQL Server Management Studio

03/23/2024 19
Starting SQL Server Management Studio

03/23/2024 PRESENTATION TITLE 20


Database Column Datatype Sample

03/23/2024 21
SQL (Structured Query Language) used to manage and
manipulate relational databases. The following are the types
of SQL commands:

1- Data Definition Language (DDL) commands:


CREATE, ALTER, DROP, TRUNCATE

2- Data Manipulation Language (DML) commands:


SELECT, INSERT, UPDATE, DELETE

3- Data Control Language (DCL) commands:


GRANT, REVOKE

03/23/2024 22
SQL Operators

• SQL Arithmetic Operators:


+ - * /

• SQL Comparison Operators:


= , <> != , > , < , >= , <=

• SQL Logical Operators:

AND, OR, NOT

03/23/2024 23
AND Truth Table

03/23/2024 24
OR Truth Table

03/23/2024 25
NOT Truth Table

03/23/2024 26
Primary Key
A primary key is a unique identifier for each record (row).
The primary key is used to enforce the integrity of the data by
ensuring that each record has a unique identifier.
The following are the conditions that a primary key must meet:
1.Uniqueness:
• Each value in the primary key column must be unique.
2.Not-null:
• The primary key values cannot be null (empty).
3.Irreversibility:
• The primary key values cannot be changed once they are assigned.

03/23/2024 27
Foreign Key
A foreign key is a field in a relational database table that is used to establish a
relationship with another table. The foreign key in one table is used to reference the
primary key in another table.
The following are the conditions that a foreign key must meet:
1. Referenced Key:
•The foreign key must reference a primary key in another table.
2. Not-null:
•The foreign key values cannot be null (empty).
3. Consistency:
•The values in the foreign key must correspond to the values in the referenced
primary key.

03/23/2024 28
PK Examples

03/23/2024 29
FK Example

03/23/2024 30
SQL Comments

03/23/2024 31
SQL Data Definition Language (DDL) commands
CREATE TABLE Example 1 :

Text : CREATE TABLE Employee


03/23/2024 32
(
CREATE TABLE Example 2 :

03/23/2024 33
Text: CREATE TABLE EmployeeProjects
CREATE View :

03/23/2024 34
Text: CREATE VIEW EmployeeInfo AS
Drop Command :

03/23/2024 35
Truncate Command :

03/23/2024 36
Alter Table Command :

03/23/2024 37
SQL Data Manipulation Language (DML) commands

1.SELECT: used to retrieve data from a database


2.INSERT: used to insert data into a table
3.UPDATE: used to modify existing data in a
table
4.DELETE: used to delete data from a table

03/23/2024 38
SELECT Statement example 1

SELECT EmployeeID, EmployeeName, Salary FROM Employee;

SELECT Statement example 2

SELECT * FROM Employee;


03/23/2024 39
SELECT Statement example 3
Where Clause

03/23/2024 40
SELECT Statement example 4
Where Clause

03/23/2024 41
SELECT Statement example 5
Where Clause and patterns Like Operator

% anyone
? any characters
character (Oracle) _ any one character (SQL
Server)

03/23/2024 42
SELECT Statement example 6
Where Clause and patterns Like Operator

03/23/2024 43
SELECT Statement example 7
Where Clause and between Operator

03/23/2024 44
SELECT Statement example 8
Where Clause and (IN) Operator

03/23/2024 45
SELECT Statement example 9
Where Clause and is NULL Operator

03/23/2024 46
SELECT Statement example 10
Using mathematical expression and alias name

SELECT EmployeeID, FirstName, Salary, Salary * 0.16 AS Tax


FROM Employee;

SELECT EmployeeID, FirstName, Salary, Salary + 50 AS Bonus


FROM Employee;

03/23/2024 47
SELECT Statement example 11
Using mathematical expression and alias name

SELECT EmployeeID, FirstName, Salary, (Salary - (Salary * 0.1)) AS Deduction


FROM Employee;

03/23/2024 48
SQL Aggregate Functions
SQL aggregate functions are functions that perform a calculation on
a set of values and return a single value. Some of the most commonly
used SQL aggregate functions are:

1. COUNT: returns the number of rows in a table


2. SUM: returns the sum of all the values in a column
3. AVG: returns the average of all the values in a column
4. MIN: returns the minimum value in a column
5. MAX: returns the maximum value in a column

03/23/2024 49
SQL Aggregate Functions Example 1

03/23/2024 50
SQL Aggregate Functions Example 2
Group By clause

03/23/2024 51
SQL Aggregate Functions Example 3

03/23/2024 52
SQL Aggregate Functions Example 4

03/23/2024 53
SQL Aggregate Functions Example 5

03/23/2024 54
SQL Aggregate Functions Example 6

03/23/2024 55
SQL Aggregate Functions Example 7
Having clause

03/23/2024 56
SQL Aggregate Functions Example 8
Relatively complex example

03/23/2024 57
Order By Clause

In SQL, ORDER BY is a clause used to sort the results of a query in


either ascending (ASC) or descending (DESC) order based on one or
more columns.
ASC specifies that the result should be sorted in ascending order,
which is the default behavior if you don't specify ASC or DESC.
DESC specifies that the result should be sorted in descending order.

03/23/2024 58
SQL Aggregate Functions Example 9
Order By

03/23/2024 59
Insert statement

03/23/2024 60
Update Statement

03/23/2024 61
Delete Statement

03/23/2024 62
Joining two Table

03/23/2024 63
Joining two Table with alias

03/23/2024 64
Joining three Table with alias

03/23/2024 65
Create User (Oracle)

03/23/2024 66
SQL Data Control Language (DCL) commands

03/23/2024 67
SQL Data Control Language (DCL) commands

03/23/2024 68
SQL (DCL) commands - Commit

03/23/2024 69
SQL (DCL) commands - Commit

03/23/2024 70
SQL (DCL) commands - Rollback

03/23/2024 71
Special Ms-Sql Server Functions

1. LEN: Returns the length of a string.


2. SUBSTRING: Returns a portion of a string.
3. CONVERT: convert from data type to another.
4.CONCAT: Concatenates two or more strings.
5. UPPER: Converts a string to uppercase.
6. LOWER: Converts a string to lowercase.
7. REPLACE: Replaces all occurrences of a specified string value
with another string value.
8. TRIM: Removes leading and trailing spaces from Field.

03/23/2024 72
SQL String Functions – Examples 1

03/23/2024 73
SQL String Functions – Examples 2

03/23/2024 74
SQL String Functions – Examples 3

03/23/2024 75
SQL String Functions – Examples 4

03/23/2024 76
SQL String Functions – Examples 5

03/23/2024 77
SQL String Functions – Examples 6

03/23/2024 78
SQL numbers Functions

03/23/2024 79
SQL numbers Functions example 1

03/23/2024 80
SQL numbers Functions example 2

03/23/2024 81
SQL numbers Functions example 3

03/23/2024 82
SQL numbers Functions example 4

03/23/2024 83
SQL numbers Functions example 5

03/23/2024 84
Oracle to_char function

03/23/2024 85
Oracle to_date function

03/23/2024 86
Oracle to_number function

03/23/2024 87
Oracle to_number function

03/23/2024 88
SQL server Date Conversion
List of Date conversion Code

03/23/2024 89
SQL server Date Conversion Examples 1

03/23/2024 90
SQL server Date Conversion Examples 2

03/23/2024 91
Case Study

SELECT
CONCAT('Employee Name: ',REPLACE(UPPER(firstname), 'A', '@'),' ' ,LastName) AS NewEmpName,
CONVERT(NVARCHAR, HireDate, 113) AS HireDate,
Salary AS OriginalSalary,
FLOOR(Salary * 0.20) AS BonusValue ,
FLOOR(SQRT(Salary)) AS DeductionValue ,
Salary + (FLOOR(Salary * 0.20) - FLOOR(SQRT(Salary))) AS NewSalary
FROM Employee
where Gender is not null
group by firstname,HireDate,Salary,LastName
having MIN(EmployeeID) > 0
order by Salary desc;

03/23/2024 92
Case Study Output

03/23/2024 93
Select data depend on another select
Get the employee who haven't projects

SELECT EmployeeID
,FirstName
,LastName FROM Employee where EmployeeID
not in (SELECT EmployeeID FROM
EmployeeProjects)

03/23/2024 94
Distinct Operator (Remove Duplicated Values)
SELECT EmployeeID FROM EmployeeProjects

SELECT distinct EmployeeID FROM EmployeeProjects

03/23/2024 95
Multiple select statement
select (select firstname from employee where EmployeeID=1) as firstname,(select
LastName from employee where EmployeeID=1) as LastName , salary
,(select sqrt(salary) from employee where EmployeeID=1) as sqrtsalary ,(select
salary * 0.15 from employee where EmployeeID=1) as '10%salary'
from employee where EmployeeID=1

03/23/2024 96
Advance Topics to Learn

03/23/2024 97
Keep practice and
practice
Good
Luck

Course Level : Beginners (University Students)


Instructor : Ahmad H. Al Kheraisat
Information Technology Center
Director Technical Assistant

You might also like