A comprehensive learning resource to help you master SQL using Microsoft SQL Server. It includes commands, syntax explanations, and practical examples — with bilingual comments in English and Arabic.
- Single-line (
--
) and multi-line (/* ... */
) comments - Data Types:
- Numeric:
int
,float
,money
, etc. - String:
char
,varchar
,nvarchar
- DateTime:
date
,time
,datetime
- Binary:
binary
,image
(deprecated) - Others:
xml
,sql_variant
- Numeric:
CREATE DATABASE CompanyCycle9;
USE CompanyCycle9;
CREATE TABLE Employee (
SSN INT PRIMARY KEY IDENTITY(1,1),
Name VARCHAR(15) NOT NULL,
Address VARCHAR(50) DEFAULT 'Cairo',
Salary MONEY,
Date DATE,
Gender CHAR(1),
LName VARCHAR(15),
SuperSSN INT REFERENCES Employee(SSN),
DNo INT
);
ALTER TABLE
,DROP TABLE
supported- Identity, Foreign Key, and NOT NULL constraints explained
INSERT INTO Employee (Name, LName, Date, Address, Gender, DNo, SuperSSN, Salary)
VALUES ('Amr', 'Nabih', '2003-23-08', 'Mansoura', 'M', 8, NULL, NULL);
UPDATE Employee
SET Address = 'Dokki'
WHERE SSN = 1;
DELETE FROM Employee
WHERE SSN = 9;
- Examples for inserting single and multiple rows
- Conditional updates and deletions
SELECT * FROM Student;
SELECT St_name + ' ' + St_Lname AS FullName
FROM Student;
SELECT * FROM Student
WHERE St_age BETWEEN 20 AND 25;
SELECT * FROM Student
WHERE St_address IN ('Cairo', 'Giza');
WHERE
,BETWEEN
,IN
,LIKE
,IS NULL
- Sorting with
ORDER BY
,DISTINCT
-- INNER JOIN
SELECT S.St_name, D.Dept_Name
FROM Student S
INNER JOIN Department D ON D.Dept_Id = S.Dept_Id;
-- LEFT OUTER JOIN
SELECT S.St_name, D.Dept_Name
FROM Student S
LEFT OUTER JOIN Department D ON D.Dept_Id = S.Dept_Id;
- All join types:
INNER
,LEFT
,RIGHT
,FULL
,SELF
SELECT * FROM Student
WHERE St_age > (SELECT AVG(St_age) FROM Student);
- Subqueries in
WHERE
,IN
, andDELETE
statements - Rewriting joins with subqueries
SELECT COUNT(*), SUM(Salary), AVG(Salary)
FROM Instructor;
SELECT LOWER(St_name), UPPER(St_Lname)
FROM Student;
SELECT FORMAT(GETDATE(), 'dd-MM-yyyy');
SELECT EOMONTH(GETDATE());
SELECT ISNULL(St_address, 'No Address') FROM Student;
SELECT COALESCE(St_name, St_Lname, 'Unknown') FROM Student;
SELECT Ins_Name, Salary,
ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RNum,
RANK() OVER (ORDER BY Salary DESC) AS Rnk,
DENSE_RANK() OVER (ORDER BY Salary DESC) AS DRnk
FROM Instructor;
- All commands tested on Microsoft SQL Server
- Bilingual comments and examples included (English + Arabic)
- Useful for both class notes and revision
- Open SQL Server Management Studio (SSMS)
- Copy each code block and run it interactively
- Modify and explore results for better understanding