SQL Notes
SQL Notes
SQL Notes
Contents
1. INTRODUCTION ......................................................................................................................................... 3
2. Installation ................................................................................................................................................ 6
3. SQL BASICS [INTRODUCTION] ................................................................................................................. 20
BASIC TYPES OF SQL STATEMENTS: ........................................................................................................ 23
SQL Comparison Operators..................................................................................................................... 24
SQL Logical Operators ............................................................................................................................. 25
SQL Operator Precedence....................................................................................................................... 26
SQL Data types ........................................................................................................................................ 26
MS SQL Server string DATA TYPES .......................................................................................................... 27
MS SQL Server numeric DATA TYPES ...................................................................................................... 27
MS SQL Server date & time DATA TYPES ................................................................................................ 28
DDL and DML EXERCISE .......................................................................................................................... 28
SCHEMA AND TABLES ............................................................................................................................. 34
SCHEMA’S EXERCISE................................................................................................................................ 34
TEMP TABLES EXERCISE .......................................................................................................................... 39
SQL Constraints ....................................................................................................................................... 40
Primary Key Constraint ........................................................................................................................... 41
Foreign Key Constraint ............................................................................................................................ 41
Not Null Constraint ................................................................................................................................. 42
Unique Key Constraint ............................................................................................................................ 42
Default Value Constraint ......................................................................................................................... 43
Check Value Constraint ........................................................................................................................... 43
Index Constraint ...................................................................................................................................... 44
pg. 1
CONSTRAINTS EXERCISE.......................................................................................................................... 44
SQL JOINS ................................................................................................................................................ 48
SQL INNER JOIN....................................................................................................................................... 49
SQL LEFT JOIN.......................................................................................................................................... 49
SQL RIGHT JOIN ....................................................................................................................................... 50
SQL FULL OUTER ..................................................................................................................................... 50
SQL SELF JOIN.......................................................................................................................................... 51
JOIN EXERCISE ......................................................................................................................................... 52
SQL VIEWS ............................................................................................................................................... 60
SQL VIEWS Exercise................................................................................................................................. 61
SQL STORED PROCEDURES...................................................................................................................... 65
SQL STORED PROCEDURES EXERCISE...................................................................................................... 67
SQL USER DEFINED FUNCTIONS .............................................................................................................. 68
SQL USER DEFINED FUNCTIONS EXERCISE .............................................................................................. 68
pg. 2
1. INTRODUCTION
1. WHAT IS DATABASE?
A PLATFORM TO STORE ANY TYPE OF DATA. ANY AMOUNT OF DATA.
2. TYPES OF DATABASES?
5. WHAT IS CLOUD?
pg. 3
A PLATFORM USED TO STORE ANY TYPE OF DATA REMOTELY EX: GOOGLE DOC EX: MOBILE CONTACTS
6. WHAT IS AZURE?
A MICROSOFT CLOUD PLATFORM TO STORE BIG DATA AND PERFORM DATA ANALYTICS. CHEAPER, EASY
TO USE, CODELESS ARCHITECTURE.
10. WHAT IS T-SQL? TRANSACT SQL. USED TO COMMUNICATE WITH DATABASES STORED IN
WIN MICROSOFT SQL SERVER. FOR DEV, TEST, ADMIN.
pg. 4
11. CAREER OPTIONS?
4. DATA ENGINEERS DESIGN & MANAGE DWH + ETL WITH AZURE DATA ENGINEER
Microsoft Certification: DP 203
pg. 5
2. Installation
STEP 1: SQL Server Installation
https://sqlschool.com/sql-server-downloads/
pg. 6
pg. 7
pg. 8
pg. 9
pg. 10
pg. 11
CLICK “ADD Current User” as administrator
pg. 12
pg. 13
STEP 2: SQL Server Installation [FOR 2ND SERVER]
From your downloaded SQL Server > Double Click the Setup file once again and repeat above
Installation Process. Give a different name for your installation [Instance Name]
Optional: Select DEVELOPER Edition during the Installation.
SERVICE ACCOUNT: A valid User from your Operating System to manage the service.
During installation, one service account is auto created.
Authentication Types:
Windows Authentication : Only Windows Users can connect to SQL Server
SQL Authentication : Any user can connect to SQL Server. For Linux, Mac..
Authentication Modes:
Windows Authentication Mode : Only Windows Users are supported to connect
Mixed Mode *** : Windows Users and SQL Server Users supported
pg. 14
pg. 15
pg. 16
pg. 17
STEP 3: INSTALL SSMS TOOL [SQL SERVER MANAGEMENT STUDIO]
Download from https://sqlschool.com/downloads/
Double click SSMS Setup File > Install.
Restart your Computer.
FOR 2ND SERVER : LEFT: CONNECT > DATABASE ENGINE > DROP DOWN : BROWSE FOR MORE >
EXPAND DATABASE ENGINE > SELECT ABOVE INSTALLED SERVER (2) > OK > CONNECT.
pg. 18
Component Purpose
1 Database Engine Supports Real-time, Rapid Processing (OLTP) Databases. We use SQL.
2 Analysis Services Supports Analytical Databases for Big Data Analysis, Forecasts
pg. 19
3 Integration Services For Design of Databases for Old, Historical Data (DATA WAREHOUSE : DWH)
4 Reporting Services For end to end implementation of Report Design, Storage, Access.
5 Machine Learning Python and R. Used to implement Machine Learning for Data Scientists
1. WHAT IS DATABASE?
2. WHAT IS DBMS?
3. WHAT IS SQL SERVER?
4. ADVANTAGES OF SQL SERVER?
pg. 20
SQL : STRUCTURED QUERY LANGUAGE. A PLATFORM USED TO COMMUNICATE WITH ANY
OS DATABASE. FOR DEVELOPMENT, TESTING AND ADMINISTRATION (MANAGEMENT).
T-SQL : TRANSACT SQL. A TYPE OF SQL EXCLUSIVELY USED FOR COMMUNICATING WITH
WIN DATABASES STORED IN MICROSOFT SQL SERVER (MS SQL SERVER).
ITEM #1: HOW TO CREATE NEW DATABASE IN YOUR LOCAL SQL SERVER?
WINDOWS APPS SEARCH > TYPE "SSMS" > LAUNCH SSMS > THIS PROMPTS FOR SERVER
NAME > IN THE DROPDOWN > BROWSE FOR MORE > EXPAND DATABASE ENGINE >
SELECT REQUIRED SERVER > OK > CONNECT.
RIGHT CLICK DATABASES FOLDER > NEW DATABASE > SPECIFY A DATABASE NAME > OK.
THEN TWO FILES ARE AUTO CREATED IN YOUR COMPUTER INSTALLATION LOCATION
1. DATA FILE = ROWS DATA FILE. USED TO STORE TABLE DATA
FILE TYPE : MDF = PRIMARY DATA FILE
NDF
2. LOG FILE = TRANSACTION LOG FILE. TO MONITOR THE DATABASE.
FILE TYPE : LDF = LOG DATA FILE
pg. 21
RIGHT CLICK TOP YELLOW RIBBON > SAVE > SET TABLE NAME > OK.
ITEM #6: LIMITATIONS WITH GUI [GRAPHICAL USER INTERFACE] IN SSMS TOOL?
1. ONLY 200 ROWS CAN BE EDITTED AT A TIME
2. ONLY 1000 ROWS CAN BE REPORTED A TIME
TO ADDRESS ALL ABOVE LIMITATIONS, WE USE SQL.
----------
ITEM #7: HOW TO CREATE DATABASE USING SQL?
FROM SSMS TOOL: RIGHT CLICK SERVER > NEW QUERY > THEN YOU SEE A "SESSION".
TYPE BELOW COMMAND. SELECT THE COMMAND AND CLICK @ EXECUTE.
CREATE DATABASE ProductDB
ITEM #9: HOW TO CREATE TABLE INSIDE ABOVE DATABASE USING SQL?
SYNTAX:
CREATE TABLE <<TABLENAME>>
(
<<Column1Name>> <<datatype>>,
<<Column2Name>> <<datatype>>,
<<Column3Name>> <<datatype>>,
....... max: upto 1023 columns
)
DataType specifies the type of value. Example: Digits, Alphabets, Date, Time, etc..
pg. 22
CREATE TABLE tblStore
(
ProductID INTEGER, -- THIS COLUMN STORES DIGITS [0, -9, 99..]
ProductName CHAR(30) -- THIS COLUMN STORES UPTO 30 DIGITS, ALPHA, SYMBOLS
)
ITEM #11: HOW TO REPORT DATA FROM THE TABLE USING SQL ?
SELECT * FROM tblStore; -- * means to report all columns
ITEM #12: HOW TO SAVE OR STORE THE ABOVE SQL QUERY IN YOUR PC FOR FUTURE USE?
TOP : FILE MENU > SAVE OR SAVE AS > SPECIFY YOUR DISK LOCATION.
SELECT Used to Report data from Table +++ Create Tables +++ Insert Data
In later classes we have other SQL Commands to practice: TCL, DCL, etc...
During Installation of SQL Server, a set of FIVE System Databases are auto created for each instance :
pg. 23
1. master : Used to manage the connections to SQL Server and other databases
3. msdb : Used by SQL DBAs for Jobs, Alerts, Monitoring, Errors and Repairs
4. tempdb : Used by Developers for Temporary Data Storage and Faster Data Access
5. resource : Hidden, internally used by master db to control SQL Server Files in OS.
SKK E C R C M
pg. 24
SQL Logical Operators
SKK E C R C M
pg. 25
SQL Operator Precedence
SKK E C R C M
SKK E C R C M
pg. 26
MS SQL Server string DATA TYPES
Data t pe Descrip on
It holds the character strin ith the ed idth Ma i u si e of this data t pe
char n is characters
It holds the character strin ith the variable idth Ma i u si e of this data
varchar n t pe is also characters
It holds the character strin ith the variable idth Ma i u si e of this data
varchar a t pe is characters
It holds the character strin ith the variable idth his data t pe can store up to
te t a i u of te t data
It holds the Unicode strin ith the ed idth Ma i u si e of this data t pe is
nchar also characters
It holds the Unicode strin ith the variable idth Ma i u si e of this data
nvarchar t pe is also characters
It holds the Unicode strin ith the variable idth his data t pe can store up to
nte t a i u of te t data
binar n It holds the binar strin ith the ed idth
It holds the binar strin ith variable idth Ma i u si e of this data t pe is
varbinar also b tes
It holds the binar strin of a len th of variable idth Ma i u si e of this
varbinar a data t pe is
It holds the variable len th of the data t pe that can store binar data Ma i u
i a e si e of this data t pe is
It holds the Unicode strin of a len th of variable idth Ma i u si e of this
varchar a data t pe is characters
SKK E C R C M
Data t pe Descrip on
bit It holds the inte er that can be or ULL
n int It allo to holds the hole nu ber fro to
s allint It allo to holds the nu ber bet een and
int It allo to holds the hole nu ber bet een and
It allo to holds the hole nu ber bet een and
bi int
It is ed precision and scale nu bers hat allo nu bers fro to
he p para eter indicates the a i u total nu ber of di its that can be stored on both
sides of the deci al point le and ri ht It ust have a value fro to default it is
deci al p s
he s para eter indicates the a i u nu ber of the di it to the ri ht of the deci al
point S ust be a value fro to p he value is set to b default
It is ed precision and scale nu bers hat allo nu bers fro to
he p para eter indicates the a i u total nu ber of di its that can be stored on both
sides of the deci al point le and ri ht It ust have a value fro to default it is
nu eric p s
he s para eter indicates the a i u nu ber of the di it to the ri ht of the deci al
point S ust be a value fro to p he value is set to b default
s all one It holds the onetar data fro to
It holds the onetar data fro to
Mone
It holds or store the oa n precession nu ber data fro E to E
loat n he n para eter indicates hether the eld should hold or b tes loat contains a
b te eld hile loat contains an b te eld he default value of n is
real It holds the oa n precision nu ber data fro E to E
SKK E C R C M
pg. 27
MS SQL Server date & time DATA TYPES
Data t pe Descrip on
It stores date and e both fro anuar to Dece ber ith an accurac of
illiseconds
It stores date and e both fro anuar to Dece ber ith an accurac of
date e
nanoseconds
It stores date and e both fro anuar to une ith an accurac of
s alldate e
inute
It stores date onl fro anuar to Dece ber
It store e onl to an accurac of nanoseconds
date eo set It is the sa e of the date e ith the addi on of the e one o set
It stores the uni ue nu ber that ets updated ever e a ro ets created or odi ed It
esta p does not correspond to real e and is based on internal e Each table a have onl one
esta p variable
SKK E C R C M
USE Employee_Database
EMP_ID INT, -- THIS COLUMN STORES DIGITS [0, -9, 99, ETC...]
pg. 28
EMP_NAME CHAR(30), -- THIS COLUMN STORES UPTO 30 CHARACTERS [DIGITS, SYMBOLS,
ALPHABETS]. FIXED DATA. USER NAME. GENDER
EMP_SAL INTEGER -- THIS COLUMN STORES DIGITS [0, -9, 99, ETC...]
INSERT INTO EMPLOYEE_INFO VALUES (1005, 'JOE', 'USA', 999999), (1006, 'ANI', 'USA', 999999);
GO
INSERT INTO EMPLOYEE_INFO VALUES (1007, 'AMI', 'USA', 777777), (1008, 'AMIN', 'INDIA', 999999);
-- STATEMENTS ABOVE "GO" FORMS ONE BATCH. STATEMENTS BELOW "GO" FORMS ANOTHER BATCH.
-- FROM ABOVE: WE HAVE TWO BATCHES. WE USE BATCHES IN REAL-WORLD TO "GROUP" 100s & 1000s
OF LINES TOGTHER.
-- ADVANTAGE OF BATCH: WHEN 1ST BATCH IS UNDER EXECUTION, 2ND BATCH GETS "READY" FOR
EXECUTION. LOOKING FOR QUERY RESOURCES: PROCESSOR, MEMORY
pg. 29
-- QUERY 6: HOW TO REPORT LIST OF EMPLOYEES FROM CANADA?
-- QUERY 8: HOW TO REPORT LIST OF EMPLOYEES EITHER FROM CANADA OR FROM USA?
-- QUERY 9: HOW TO REPORT LIST OF EMPLOYEES NEITHER FROM CANADA NOR FROM USA?
-- QUERY 10:HOW TO REPORT LIST OF EMPLOYEES WHOSE SALARY VALUE IS BETWEEN 777777 AND
999999?
-- QUERY 11:HOW TO REPORT LIST OF EMPLOYEES WHOSE SALARY VALUE IS NOT BETWEEN 777777
AND 999999?
SELECT * FROM EMPLOYEE_INFO WHERE EMP_SAL NOT BETWEEN 777777 AND 999999
-- QUERY 13: HOW TO REPORT EMPLOYEES WHOSE NAME STARTS WITH LETTER 'S'?
-- QUERY 14: HOW TO REPORT EMPLOYEES WHOSE NAME DOES NOT STARTS WITH LETTER 'S'?
-- QUERY 15: HOW TO REPORT EMPLOYEES WHOSE NAME STARTS WITH LETTERS 'SA'?
pg. 30
-- QUERY 16: HOW TO REPORT EMPLOYEES WHOSE NAME CONTAINS LETTER 'A'?
-- QUERY 17: HOW TO REPORT EMPLOYEES WHOSE NAME ENDS WITH LETTER 'N'?
-- QUERY 18: HOW TO REPORT EMPLOYEES WITH SALARIES IN ASCENDING ORDER? MEANS, LOW TO
HIGH
-- QUERY 19: HOW TO REPORT EMPLOYEES WITH SALARIES IN DESCENDING ORDER? MEANS, HIGH TO
LOW
-- QUERY 20: HOW TO REPORT TOP 2 EMPLOYEES BASED ON THEIR IDs. IN ASCENDING ORDER.
MEANS, LOW TO HIGH
-- QUERY 21: HOW TO REPORT ALL EMPLOYEES BASED ON THEIR IDs. IN ASCENDING ORDER EXCEPT
TOP 2?
-- QUERY 23: HOW TO REPORT HIGHEST SALARY VALUE OUT OF OF ALL EMPLOYEES?
pg. 31
SELECT EMP_ID,EMP_NAME,EMP_COUNTRY,MAX(EMP_SAL) AS MAXIMUM_SALARY FROM
EMPLOYEE_INFO
GROUP BY EMP_ID,EMP_NAME,EMP_COUNTRY,EMP_SAL
GROUP BY EMP_ID,EMP_NAME,EMP_COUNTRY,EMP_SAL
-- QUERY 24: HOW TO REPORT LIST OF EMPLOYEES WITH HIGHEST (MAXIMUM) SALARY?
-- QUERY 25: HOW TO REPORT LIST OF EMPLOYEES WITH LOWEST (MINIMUM) SALARY?
-- SUB QUERY: TO DEFINE ONE QUERY INSIDE ANOTHER. THE QUERY INCLUDED IN ( ) WILL BE
EXECUTED FIRST.
-- QUERY 27: HOW TO REMOVE THE DATA IN EXISTING TABLE? -- Delete is monitored in the log
file. Supports Conditions. Safe.
-- QUERY 28: HOW TO REMOVE THE DATA IN EXISTING TABLE? -- Truncate is NOT monitored in
the log file. Does not support conditions. Faster.
pg. 32
SELECT * FROM EMPLOYEE_INFO
-- WE WILL SEE EMPTY TABLE.
pg. 33
SCHEMA AND TABLES
SKK E C R C M
SCHEMA’S EXERCISE
USE DBTEST
pg. 34
COL1 INT,
COL2 CHAR
INSERT INTO TABLE0 VALUES (1, 'ABC') -- ERROR. REASON: WE CAN INSERT MAX 1
CHARACTER IN 2ND COLUMN
INSERT INTO TABLE0 VALUES (1, 'A') -- NO ERROR. REASON: WE CAN INSERT
1 CHARACTER IN 2ND COLUMN
--- QUERY 6: IN REAL-TIME, EVERY DATABASE HAS HUGE NUMBER OF TABLES. SO WE NEED TO GROUP
THOSE TABLES INTO SCHEMAS
pg. 35
-- QUERY 7: HOW TO USE ABOVE SCHEMA AND CREATE TABLES?
COL1 INT,
COL2 INT
COL1 INT,
COL2 INT
COL1 INT,
COL2 INT
pg. 36
-- QUERY 8: HOW TO CREATE ONE MORE SCHEMA IN THE DATABASE? WE CAN CREATE ANY
NUMBER OF SCHEMAS INSIDE THE DATABASE
COL1 INT,
COL2 INT
COL1 CHAR,
COL2 INT
COL1 INT,
COL2 CHAR,
COL3 FLOAT
pg. 37
INSERT INTO SCHEMA2.TABLE1 VALUES (1, 1)
-- QUERY 12: HOW TO MOVE OR MIGRATE TABLE FROM ONE SCHEMA TO ANOTHER?
pg. 38
TEMP TABLES EXERCISE
CREATE TABLE #TABLE1 -- THIS IS A LOCAL TEMPORARY TABLE. you create, you
use it
COL1 INT,
COL2 INT
COL1 INT,
COL2 INT
-- FROM SSMS TOOL: GO TO FILE > NEW > QUERY WITH CURRENT CONNECTION. OR RIGHT CLICK
SERVER > NEW QUERY. RUN BELOW STATEMENTS:
SELECT * FROM #TABLE1 -- THIS STATEMENT WILL NOT WORK. REASON: ITS LOCAL TO
CURRENT SESSION (THIS QUERY WINDOW).
SELECT * FROM ##TABLE2 -- THIS STATEMENT WILL WORK. REASON: ITS GLOBAL TO
ENTIRE SERVER.
-- HOW TO COPY DATA FROM ONE TABLE TO ANOTHER TABLE? -- COPY PASTE
pg. 39
-- ABOVE TABLE "NEW_TABLE" IS AUTO CREATED IN THE CURRENTLY CONNECTED DATABASE.
-- PERMENANT TABLE.
SQL Constraints
Constraints are the rules enforced on data colu ns on a table hese are used to li it the t pe of data that can o into a ta ble his ensures the
accurac and reliabilit of the data in the database
Constraints can either be colu n level or table level Colu n level constraints are applied onl to one colu n hereas table level constraints are
applied to the en re table
ollo in are so e of the ost co onl used constraints available in S L
SKK E C R C M
pg. 40
Primary Key Constraint
SKK E C R C M
SKK E C R C M
pg. 41
Not Null Constraint
SKK E C R C M
SKK E C R C M
pg. 42
Default Value Constraint
SKK E C R C M
SKK E C R C M
pg. 43
Index Constraint
SKK E C R C M
CONSTRAINTS EXERCISE
CREA E A LE tblCourses
pg. 44
I SER I tblCourses VALUES 'C M U ERS'
SELEC * R M tblCourses
CREA E A LE tblStudents
StdCourseID int RE ERE CES tblCourses CourseID EVERY S UDE SH ULD E E R LLED I
VALID EXIS I C URSE LY
pg. 45
I SER I tblStudents VALUES 'RAMI' ERR R
SELEC * R M tblStudents
CREA E A LE tblSta
StfCourseID int
RU CA E A LE tblSta
pg. 46
AL ER A LE tblSta ADD C S RAI K_S ID RIMARY KEY StfID
AL ER A LE tblSta ADD C S RAI K_S CRSID REI KEY StfCourseID RE ERE CES
tblCourses CourseID
SELEC * R M tblSta
SELEC * R M tblSta
Inde
pg. 47
SQL JOINS
SKK E C R C M
SKK E C R C M
pg. 48
SQL INNER JOIN
SKK E C R C M
pg. 49
SQL RIGHT JOIN
SKK E C R C M
SKK E C R C M
pg. 50
SQL SELF JOIN
SKK E C R C M
pg. 51
JOIN EXERCISE
USE UNIVDATABASE4
CourseName varchar(30) not null, -- THIS COLUMN DOES NOT ALLOW NULL VALUES. MEANS,
MANDATORY COLUMNS
INSERT INTO tblCourses VALUES (1001, 'COMPUTERS', 120), (1002, 'ROBOTICS', 180), (1003, 'CIVIL', 180)
StdID int PRIMARY KEY, -- THIS COLUMN DOES NOT ALLOW DUPLICATES, DOES
NOT ALLOW NULL VALUES
StdName varchar(30) not null, -- THIS COLUMN DOES NOT ALLOW NULL VALUES. MEANS,
MANDATORY COLUMNS
pg. 52
StdCourseID int REFERENCES tblCourses(CourseID) -- EVERY STUDENT SHOULD REGISTER
FOR A VALID COURSE ONLY
INSERT INTO tblStudents VALUES (10001, 'SAI', 30, 1001) -- Mr. Sai is registering
for computers course
INSERT INTO tblStudents VALUES (10004, 'SAI2', 30, 1001) -- Mr. Sai2 is registering for
computers course
SELECT * FROM
pg. 53
INNER JOIN
ON
tblStudents.StdCourseID = tblCourses.CourseID
-- REQ 2: HOW TO REPORT LIST OF COURSES WITH STUDENTS & COURSES WITHOUT STUDENTS?
MATCHING DATA & MISSING DATA
SELECT * FROM
LEFT OUTER JOIN -- LEFT OUTER JOIN : All Left Table + Matching Right Table Data.
Non match RIGHT data is null
ON
tblStudents.StdCourseID = tblCourses.CourseID
-- REQ 3: HOW TO REPORT LIST OF COURSES WITH STUDENTS & COURSES WITHOUT STUDENTS?
MATCHING DATA & MISSING DATA
SELECT * FROM
RIGHT OUTER JOIN -- RIGHT OUTER JOIN : All RIGHT Table + Matching LEFT Table Data. Non
match LEFT data is null
ON
tblStudents.StdCourseID = tblCourses.CourseID
pg. 54
-- REQ 4: HOW TO REPORT LIST OF COURSES WITHOUT STUDENTS?
MISSING DATA
SELECT * FROM
LEFT OUTER JOIN -- LEFT OUTER JOIN : All Left Table + Matching Right Table Data.
Non match RIGHT data is null
ON
tblStudents.StdCourseID = tblCourses.CourseID
------ Exercise 2
USE RESERVATIONDB
CRAFT_CODE VARCHAR(30),
DESTINATION VARCHAR(30)
pg. 55
)
CRAFT_CODE varchar(30),
No_of_Seats int ,
Class_Code varchar(10)
pg. 56
INSERT INTO Reservation VALUES ('EMI104', 2, 'BIZ')
ON
FLIGHT.CRAFT_CODE = Reservation.CRAFT_CODE
Reservation
ON
FLIGHT.CRAFT_CODE = Reservation.CRAFT_CODE
pg. 57
FLIGHT
ON
FLIGHT.CRAFT_CODE = Reservation.CRAFT_CODE
RESERVATION
ON FLIGHT.CRAFT_CODE = Reservation.CRAFT_CODE
Reservation
ON
FLIGHT.CRAFT_CODE = Reservation.CRAFT_CODE
-- REQ 4: HOW TO REPORT LIST OF FLIGHTS WITH & WITHOUT RESERVATIONS, RESERVATIONS
WITH & WITHOUT FLIGHTS? FULL OUTER JOIN
Reservation
ON
pg. 58
FLIGHT.CRAFT_CODE = Reservation.CRAFT_CODE
-- REQ 5: HOW TO REPORT LIST OF FLIGHTS & RESERVATIONS. REPORT ALL POSSIBLE
COMBINATIONS? CROSS JOIN
/*
LEFT : REPORT ALL LEFT, MATCHING RIGHT. NON MATCH RIGHT IS NULL
RIGHT : REPORT ALL RIGHT, MATCHING LEFT. NON MATCH LEFT IS NULL
*/
pg. 59
SQL VIEWS
SKK E C R C M
SKK E C R C M
pg. 60
SKK E C R C M
-- EXAMPLE 1:
USE DBOBJECT1
pg. 61
COURSE_DUR INT CHECK (COURSE_DUR = 120 OR COURSE_DUR = 180)
INSERT INTO tblCourses VALUES (101, 'COMPUTERS', 120), (102, 'ROBOTICS', 120), (103, 'CIVIL', 180)
INSERT INTO tblStudent VALUES (1001, 'SAI', 39, 101), (1002, 'AMI', 39, 101), (1003, 'AMIN', 39, 101)
INSERT INTO tblStudent VALUES (1004, 'JOHN', 39, 102),(1005, 'JEFFERY', 39, 102)
pg. 62
tblStudent
ON
tblCourses.COURSE_ID = tblStudent.StdCourse
tblStudent
ON
tblCourses.COURSE_ID = tblStudent.StdCourse
WHERE
tblCourses.COURSE_NAME = 'COMPUTERS'
INSERT INTO tblStudent VALUES (1006, 'ARUN', 35, 103), (1007, 'DAVID', 39, 101)
-- QUERY 8: HOW TO STORE ABOVE QUERY IN THE DATABASE FOR EASY ACCESS?
AS
tblStudent
ON
tblCourses.COURSE_ID = tblStudent.StdCourse
WHERE
pg. 63
tblCourses.COURSE_NAME = 'COMPUTERS'
-- QUERY 10: HOW TO REPORT LIST OF STUDENTS FROM ROBOTICS COURSE? USING VIEW?
AS
tblStudent
ON
tblCourses.COURSE_ID = tblStudent.StdCourse
WHERE
tblCourses.COURSE_NAME = 'ROBOTICS'
pg. 64
SQL STORED PROCEDURES
SKK E C R C M
pg. 65
pg. 66
SQL STORED PROCEDURES EXERCISE
-- QUERY 12: HOW TO REPORT LIST OF STUDENTS FROM A GIVEN COURSE?
AS
tblStudent
ON
tblCourses.COURSE_ID = tblStudent.StdCourse
WHERE
tblCourses.COURSE_NAME = @CourseName
pg. 67
SQL USER DEFINED FUNCTIONS
SKK E C R C M
AS
RETURN
tblStudent
ON
tblCourses.COURSE_ID = tblStudent.StdCourse
pg. 68
WHERE
tblCourses.COURSE_NAME = @CourseName
pg. 69