SlideShare a Scribd company logo
1
ABCCompaniesLimited
CUSTOMERS: Name, Age, Gender, Email Id, Loyalty program member, etc
EMPLOYEES: Name, Age, Gender, Email Id, Title, Pay Scale, Hire date, etc
SUPPLIERS: Name, Address, Contact, Product ID, Price, Units, etc
SHAREHOLDERS: Name, Number of shares owned, percentage of total equity, etc
CREDITORS: Name, type, Days, etc
PRODUCTS: Id, Category, MRP, Units, etc
STORES: ID, Location, Type, Area, etc
SALES: Current Year, Previous Year, YOY, YTD, etc
Let us consider a large retailer
named ABC Companies limited. The
data for such a retailer would
majorly be about its customers,
employees, suppliers, shareholders,
creditors, products, stores and sales
as shown:
These facts, ex: Name-Vin, Age-26,
Units-40, Price-$34.95, etc, or in
other words, these set of values with
respect to the variables is Data.
What is Data?
2
Organized Data
A few product Id’s and Units at a particular store of ABC Companies Ltd is as follows:
FD139, 250, FD760 , 375, JH565, 410, KU206, 190, SY408, 200, BK213, 255, FR908, 384, SL650, 293, ZM987,
212, LK209, 368
This form of representation of data becomes confusing when the number of products increase.
Representation of data in a tabular form as shown is easy to interpret and analyse the data.
Product
Id
Units
FD139 250
FD760 375
JH565 410
KU206 190
SY408 200
BK213 255
FR908 384
SL650 293
ZM987 212
LK209 368
Suppose we have data for 1000 product Ids and the company wants to know the product
Ids having more than 250 units.
Unorganized data: Will take a lot of time and resource to gather the required information
Organized data in ascending order of units: Will be very quick and easy to sort out the
required information.
3
Database
STORE
Store_ID
Store_Code
Store_Name
Store_Region_ID
Store_Type
STORE BILLING
Bill_Number
Customer_ID
EmployeeID
Store_Billing_Date
Store_Billing_Time
Store_Start_Date
Store_ID
PRODUCTS
ProductID
ProductName
SupplierID
CategoryID
Product Code
Product Country
BrandID
More…
BRAND
BrandID
CategoryID
Brand_Description
STORE REGION
Store_Region_ID
Store_Region_Nmae
Store_County
Store_State
DEPARTMENT
Emp_Department_ID
Department Name
Department Code
Department Description
Department Head
Department_Start_Date
EMPLOYEES
EmployeeID
Emp_Department_ID
Manager_ID
LastName
FirstName
Title
DOB
HireDate
SUPPLIER_TYPE
Supplier_Type_ID
Supplier_Type
Supplier_Description
SUPPLIERS
SupplierID
Supplier_Type_ID
Supplier_Name
Supplier_Location_Manager
Supplier_Region
STORE BILLING
DETAILS
Bill_Number
ProductID
UnitPrice
Quantity
Total Amount
Discount
Storage cost per item
Cost price per item
Labour cost per item
PACKAGING
PackageID
Package_Description
Package_Type
CATEGORIES
CategoryID
CategoryName
Description
Picture
REGION
RegionID
Customers_Region_Description
CUSTOMERS
CustomerID
Customer_Type_ID
Customer_Region_ID
Customer_Shopper_ID
Customer_Full_Name
Customer_First_Name
Customer_Last_Name
Address
City
Region
TERRITORIES
TerritoryID
Customer_Territory_Description
RegionID
CUSTOMER TYPE
Customer_Type_ID
Customer_Type
Customer_Description
Database can be
considered as a cupboard
of organized data.
Each drawer of the
cupboard represents a table
also called an Entity.
Ex: Employees, Products,
Packaging, etc
Each Entity has variables
known as Fields.
Ex: EmployeeID, Title,
DOB, etc are the fields in
the Employees Entity.
Each field has values
known as Data.
Ex: BrandID CategoryID
162 SD125
208 GF326
363 FF118
284 DP251
189 RM318
DATA
4
RELATIONAL DATABASE
Each Table/Entity is a Relation as it
contains related data.
Suppose I need a list of Customers with
their ID, Type and store billing details.
• In order to get the customer type, there
needs to be a relation between
Customers and Customer type which is
established based on a common field
which is ‘Customer_Type_ID’.
• Similarly, to get the store billing details,
there should be a relation between
Customers, Store billing and Store
billing details based on a common field
which is ‘CustomerID’ and
‘Bill_Number’ respectively.
A database which lets us look at any of the
entities and allows us to derive more
information through its relation with other
entities is known as a Relational Database.
Note: Fields in Bold are Primary Key 5
Relational Database Management System
• RDBMS is a software that can be considered as a gatekeeper to relational database.
• It provides users and programmers with a systematic way to create, retrieve, update and delete data in a
database.
• It ensures that data is consistently organized and remains easily accessible.
Relational Database RDBMS User
ABC Companies Limited
App
6
STRUCTURED QUERY LANGUAGE (SQL)
The language used to interact
with a relational database is
known as Structured Query
Language (SQL).
The commands in SQL are
classified in three groups as
shown
Suppose we want to create a
database called ‘ABCretail’.
We can use the following
commands to create, see and
select the database
respectively.
SQLCommands
Data Definition Language
(DDL)
Data Manipulation Language
(DML)
Data Control Language
(DCL)
CREATE
ALTER
DROP
SELECT
UPDATE
INSERT
DELETE
GRANT
REVOKE
Create database ABCretail; >>This will create a database called ‘ABCretail’
Use ABCretail; >>This will select ‘ABCretail’ as the database for data retrieval.
Drop database ABCretail; >> This will delete ‘ABCretail’ database.
*SQL Commands in blue 7
Data Definition Language
CREATE TABLE command creates an entity structure in the database by defining the following:
• Entity Name: SQL throws error for names with space character. Putting the name within square brackets [ ]
works well for names with space character.
• Field Name: Putting the name within square brackets [ ] works well for names with space character.
• Field data type: The type of data that shall be going within the Column is defined.
• Primary key: It is a field which uniquely identidies each record. It can not have Null values. There can be only
one primary key in a table which can be either on one field or more than one field.
Basic Syntax:
Create table table name (field1 datatype, field2 datatype, field3 datatype, .....fieldN datatype, PRIMARY KEY (one or
more columns ));
8
Ex: Let us create the following entities:
Create table Suppliers (SupplierID int primary key not null, Supplier_Type_ID int, Supplier_Name varchar(100), Supplier_Location_Manager
varchar(100), Supplier_Region varchar(75));
Create table [Store Region] (Store_Region_ID int primary key not null, Store_Region_Name varchar(100), Store_County varchar(50),
Store_State varchar(100));
Create table Store (Store_ID int primary key not null, Store_Code int, Store_Name varchar (75), Store_Region_ID int references [Store
Region] (Store_Region_ID), Store_Type varchar (50));
• The relation between Store Region and Store is based on a common field ‘Store_Region_ID’.
• It acts as a cross-reference between tables because it references the primary key of another table which in this case is Store
Region, thereby establishing a link between them.
• Such a common field providing link between two tables is known as Foreign key.
Data Definition Language [Cont..]
SUPPLIERS
SupplierID
Supplier_Type_ID
Supplier_Name
Supplier_Location_Manager
Supplier_Region
STORE REGION
Store_Region_ID
Store_Region_Name
Store_County
Store_State
STORE
Store_ID
Store_Code
Store_Name
Store_Region_ID
Store_Type
9
Data Definition Language [Cont..]
ALTER TABLE command is used to add, delete or modify fields in an existing entity.
• Let us add a field ‘Supplier_Contact_info’ to the Suppliers entity.
Alter table Suppliers add Supplier_Contact_Info varchar(50);
• Drop field ‘Store_County’ from Store Region.
Alter table [Store Region] drop Store_County;
• Let us modify the datatype of Store_ID from int (as defined in the previous slide) to smallint.
Alter table Store modify column Store_ID smallint;
DROP TABLE command removes the table definition and all the data for that table. Once a table is deleted then
all the information available in that table will also be lost forever.
• Suppose we want to drop Store Region
Drop table [Store Region];
10
Data MANIPULATION Language
Consider the following entities populated with data as shown below:
SUPPLIERS
SupplierID Supplier_Type_ID Supplier_Name Supplier_Location
_Manager
Supplier_Region
3589 58 Zai Inc David Atlantic
5871 79 Johr services Chris Ontario
8746 34 Imi Labs Bob Quebec
4812 15 Yirtel Inc Angela Ontario
9578 46 Voda services Sara Quebec
STORE
Store_ID Store_Code Store_Name Store_Region_ID Store_Type
1547 15 Monezis 258 Speciality
2624 71 Campust 358 Super market
5455 89 Leastprice 879 Discount
3879 69 Bacabus 157 Super market
6894 37 Medicas 451 Drug store
STORE REGION
Store_Region_ID Store_Region_Name Store_County Store_State
451 Atlantic Halifax Nova
Scotia
879 Quebec Montreal Quebec
358 Ontario Peterborough Ontario
157 Atlantic Charlottetown Prince
Edward
Island
258 Ontario Simcoe Ontario
*1
*2
*3
*4
*5
a
11
Data MANIPULATION Language [CONT..]
SELECT command is used to retrieve data from database entities.
Select SupplierID, Supplier_Type_ID, Supplier_Name from Suppliers >> This retrieves all the records for fields SupplierID,
Supplier_Type_ID and Supplier_Name from Suppliers entity.*1
Select * from Suppliers >> This gets all the records for all the fields in the entity. The result is the whole suppliers entity as
shown in previous slide.
Select top 2 * from [Store Region] >> Returns a (Refer slide 13)
SELECT command with conditional statements using WHERE, LIKE, AND, OR clauses.
Select * from Suppliers where SupplierID = 8746 >> Returns *2
Select Store_Region_ID, Store_Region_Name from [Store Region] where Store_County = ‘Peterborough’ >> Returns *3
Select * from Store where Store_Name like ‘M%’ and Store_Code = 15 >> Looks for records where Store_Name starts with
‘M’ and Store_Code is 15 and returns *4
Select * from Store where Store_Type = ‘Super market’ or Store_Code = 71 >> Looks for records where Store_Name starts
with ‘M’ or Store_Code is 15 and returns *5
12Note: * refers to slide 11
Data MANIPULATION Language [CONT..]
SELECT command with conditional statements using WHERE, GROUP BY, HAVING, ORDER BY clauses.
As per Store Billing Details,
Suppose we need a list of unique
bill_number with their respective
sum of [Total Amount] with the
following conditions:
1) UnitPrice > 16.00
2) Sum of [Total Amount] > 110
in descending order of Bill_Number.
Select Bill_Number, sum([Total Amount]) from [Store Billing Details]
where UnitPrice > 16
group by Bill_Number
having sum([Total Amount]) > 110
order by Bill_Number Desc
STORE BILLING DETAILS
Bill_Number ProductID UnitPrice Quantity Total
Amount
Discount Storage cost
per item
Cost price per
item
Labour
cost per
item
12345 11 20.99 5 104.95 3% 0.75 14.50 1.50
67891 25 15.49 2 30.98 2% 1.00 9.90 1.50
01112 48 121.89 1 121.89 5% 5.00 60.50 15.00
12345 67 78.45 2 156.9 5% 2.0 50.00 5.00
51617 83 35.55 3 106.65 2% 2.50 15.00 4.00
01112 57 90.99 1 90.99 5% 2.50 65.49 10.00
Bill_Number Sum(Total Amount)
12345 261.85
01112 212.88
13
Data MANIPULATION Language [CONT..]
INSERT INTO command is used to add new rows of data to an entity in the database.
Let us add 2 more rows
of data to
Store Billing Details
as shown in previous slide
Insert into [Store Billing Details] (Bill_Number, PoductID, UnitPrice, Quantity, [Total Amount], Discount,
[Storage cost per item], [Cost price per item], [Labour cost per item]) values
(25987, 35, 52.49, 3, 157.47, ‘5%’, 2.50, 35.50, 2.00),
(65874, 88, 35.55, 3, 106.65, ‘2%’, 2.50, 16.00, 3.00)
STORE BILLING DETAILS
Bill_Number ProductID UnitPrice Quantity Total
Amount
Discount Storage cost
per item
Cost price per
item
Labour cost
per item
12345 11 20.99 5 104.95 3% 0.75 14.50 1.50
67891 25 15.49 2 30.98 2% 1.00 9.90 1.50
01112 48 121.89 1 121.89 5% 5.00 60.50 15.00
12345 67 78.45 2 156.9 5% 2.00 50.00 5.00
51617 83 35.55 3 106.65 2% 2.50 15.00 4.00
01112 57 90.99 1 90.99 5% 2.50 65.49 10.00
25987 35 52.49 3 157.47 5% 2.50 35.50 2.00
65874 88 35.55 3 106.65 2% 2.50 16.00 3.00
14
Data MANIPULATION Language [CONT..]
UPDATE and DELETE commands are used to modify and delete the existing records in an entity respectively.
1) We want to change the
discount percentage of
the highlighted cell to 10%.
2) We want to keep only
those records where
quantity is less than 2.
3) Delete all records from table.
1) Update [Store Billing Details] set Discount = ’10%’ where Bill_Number = 01112 and ProductID = 48
2) Delete from [Store Billing Details] where Quantity < 2
3) Delete from [Store Billing Details]
STORE BILLING DETAILS
Bill_Number ProductID UnitPrice Quantity Total Amount Discount Storage cost
per item
Cost price
per item
Labour cost
per item
12345 11 20.99 5 104.95 3% 0.75 14.50 1.50
67891 25 15.49 2 30.98 2% 1.00 9.90 1.50
01112 48 121.89 1 121.89 5% 5.00 60.50 15.00
12345 67 78.45 2 156.9 5% 2.00 50.00 5.00
51617 83 35.55 3 106.65 2% 2.50 15.00 4.00
01112 57 90.99 1 90.99 5% 2.50 65.49 10.00
25987 35 52.49 3 157.47 5% 2.50 35.50 2.00
65874 88 35.55 3 106.65 2% 2.50 16.00 3.00
STORE BILLING DETAILS
Bill_Number ProductID UnitPrice Quantity Total Amount Discount Storage
cost per
item
Cost price
per item
Labour cost
per item
01112 48 121.89 1 121.89 5% 5.00 60.50 15.00
01112 57 90.99 1 90.99 5% 2.50 65.49 10.00
15
Data Control Language
Data control language commands are used by database administrator or owner of the database object
to provide/remove privileges on a database object.
GRANT command is used by database administrator to provide access or privileges on the database
objects to the users.
Grant select on Employees to user1 >> This command grants a SELECT permission on Employees
entity to user1.
REVOKE command removes user access rights or privileges to the database objects.
Revoke select on Employees from user1 >> This command will revoke a SELECT privilege on
employee entity from user1.
16
JOINS
In order to derive more information through entities relation with each other, we need to combine
entities based on common fields using JOIN clause.
From the above two entities, let’s say, we want to know all the store Id’s with their Name, type and state.
Select Store_ID, Store_Name, Store_Type, Store_State from [Store Region] join Store on [Store Region].Store_Region_ID = Store. Store_Region_ID
>>
17
STORE
Store_ID Store_Code Store_Name Store_Region_ID Store_Type
1547 15 Monezis 258 Speciality
2624 71 Campust 358 Super market
5455 89 Leastprice 879 Discount
3879 69 Bacabus 157 Super market
6894 37 Medicas 451 Drug store
4426 25 Medicas 554 Drug store
7413 46 Leastprice 631 Discount
STORE REGION
Store_Region_ID Store_Region_Name Store_County Store_State
451 Atlantic Halifax Nova Scotia
879 Quebec Montreal Quebec
358 Ontario Peterborough Ontario
157 Atlantic Charlottetown Prince Edward
Island
258 Ontario Simcoe Ontario
Store_ID Store_Name Store_Type Store_State
1547 Monezis Speciality Ontario
2624 Campust Super market Ontario
5455 Leastprice Discount Quebec
3879 Bacabus Super market Prince Edward
Island
6894 Medicas Drug store Nova Scotia
This reflects the common field on which the join is
based. In this case, Store_Region_ID is a common
field where it is a primary key in Store Region and a
foreign key in Store.
Types of JOINS
Inner Join: Returns only those set of records that match in both the entities. Inner Join is same
as join, as explained in the previous slide.
Left Join: Returns all rows from the left entity, even if there are no matches in the right entity.
Select Store_ID, Store_State from Store left join [Store Region] on Store.Store_Region_ID = [Store Region].Store_Region_ID
>>
Right Join: Returns all rows from the right entity, even if there are no matches in the left entity.
Select Store_ID, Store_State from Store right join [Store Region] on Store.Store_Region_ID = [Store Region].Store_Region_ID
Full Join: Returns all the records from both the entities. It is a combination of the result of both left
join and right join.
Select Store_ID, Store_State from Store full join [Store Region] on Store.Store_Region_ID = [Store Region].Store_Region_ID
18
Store_ID Store_State
1547 Ontario
2624 Ontario
5455 Quebec
3879 Prince Edward Island
6894 Nova Scotia
4426 Null
7413 Null
Store_ID Store_State
6894 Nova Scotia
5455 Quebec
2624 Ontario
3879 Prince Edward Island
1547 Ontario
Why do we use SQL when we have Excel?
• Considering the huge database of ABC Companies Limited (refer slide 4), with some entities like
store billing going up to millions of records, Excel won’t last a few hours before it comes unusable.
• Let’s say we want to know the number of units of a particular product broken down by store and
how many of those customers are end users. In a SQL database, that’s a fairly easy query to write
and we will retrieve data in seconds.
Doing the same in excel might be arduous and time consuming.
• SQL separates analysis from data. When using SQL, data is stored separately from analysis.
That saves us from emailing large excel files. Instead send small text files having the instructions
for analysis.
Also saves from having to manage file versions or risk corrupting the data.
19
APPENDIX
DATATYPES
20
DATA TYPE FROM TO
bigint -9,223,372,036,854,775,808 9,223,372,036,854,775,807
int -2,147,483,648 2,147,483,647
smallint -32,768 32,767
tinyint 0 255
bit 0 1
decimal -10^38+1 10^38-1
numeric -10^38+1 10^38-1
money -922,337,203,685,477.5808 +922,337,203,685,477.5807
smallmoney -214,748.3648 +214,748.3647
Exact Numeric Data Types
APPENDIX
DATATYPES
21
DATA TYPE Description
char Maximum length of 8,000 characters.(Fixed length non-Unicode characters)
varchar Maximum of 8,000 characters.(Variable length non-Unicode data)
varchar(max) Maximum length of 231 characters. Variable length non-Unicode data (SQL Server 2005 only).
text Variable length non-Unicode data with a maximum length of 2,147,483,647 characters.
Character Strings Data Types
DATA TYPE Description
nchar Maximum length of 4,000 characters.(Fixed length Unicode)
nvarchar Maximum of 4,000 characters.(Variable length Unicode)
nvarchar(max) Maximum length of 231 characters. Variable length Unicode (SQL Server 2005 only).
ntext Maximum length of 1,073,741,823 characters. (Variable length Unicode)
Unicode Character Strings Data Types
22
All the commands (Highlighted in blue) work for MS SQL Server.
Thank You
Created by Pavani Ganti

More Related Content

What's hot

Intro to tsql unit 4
Intro to tsql   unit 4Intro to tsql   unit 4
Intro to tsql unit 4Syed Asrarali
 
SQL Portfolio
SQL PortfolioSQL Portfolio
SQL Portfolio
beehkae
 
Dynamic websites(lec1)
Dynamic websites(lec1)Dynamic websites(lec1)
Dynamic websites(lec1)
Belal Arfa
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
Mukesh Tekwani
 
Greg Lewis SQL Portfolio
Greg Lewis SQL PortfolioGreg Lewis SQL Portfolio
Greg Lewis SQL Portfolio
gregmlewis
 
Manufacturing and sales management system
Manufacturing and sales management systemManufacturing and sales management system
Manufacturing and sales management system
Smit Patel
 

What's hot (10)

Intro to tsql unit 4
Intro to tsql   unit 4Intro to tsql   unit 4
Intro to tsql unit 4
 
SQL Portfolio
SQL PortfolioSQL Portfolio
SQL Portfolio
 
Dynamic websites(lec1)
Dynamic websites(lec1)Dynamic websites(lec1)
Dynamic websites(lec1)
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
 
Sql wksht-2
Sql wksht-2Sql wksht-2
Sql wksht-2
 
Sql ch 5
Sql ch 5Sql ch 5
Sql ch 5
 
Greg Lewis SQL Portfolio
Greg Lewis SQL PortfolioGreg Lewis SQL Portfolio
Greg Lewis SQL Portfolio
 
Sql wksht-3
Sql wksht-3Sql wksht-3
Sql wksht-3
 
SQL(database)
SQL(database)SQL(database)
SQL(database)
 
Manufacturing and sales management system
Manufacturing and sales management systemManufacturing and sales management system
Manufacturing and sales management system
 

Similar to Practical guide to SQL basics

Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
Amin Choroomi
 
AimTo give you practical experience in database modelling, no.docx
AimTo give you practical experience in database modelling, no.docxAimTo give you practical experience in database modelling, no.docx
AimTo give you practical experience in database modelling, no.docx
simonlbentley59018
 
Intro To TSQL - Unit 4
Intro To TSQL - Unit 4Intro To TSQL - Unit 4
Intro To TSQL - Unit 4
iccma
 
IT301-Datawarehousing (1) and its sub topics.pptx
IT301-Datawarehousing (1) and its sub topics.pptxIT301-Datawarehousing (1) and its sub topics.pptx
IT301-Datawarehousing (1) and its sub topics.pptx
ReneeClintGortifacio
 
Iowa liquor sales
Iowa liquor salesIowa liquor sales
Iowa liquor sales
Trushita Redij
 
SQL.pptx
SQL.pptxSQL.pptx
Warranty management system
Warranty management systemWarranty management system
Warranty management systemMoin Raza Khan
 
SQL Select Distinct Statement
SQL Select Distinct StatementSQL Select Distinct Statement
SQL Select Distinct Statement
Select Distinct Limited
 
Lamar University - MISY 3310 Access ProjectPremiere Products.docx
Lamar University - MISY 3310 Access ProjectPremiere Products.docxLamar University - MISY 3310 Access ProjectPremiere Products.docx
Lamar University - MISY 3310 Access ProjectPremiere Products.docx
DIPESH30
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexSalesforce Developers
 
Physical data modeling
Physical data modelingPhysical data modeling
Physical data modeling
Abbas Ahmed
 
Database optimization
Database optimizationDatabase optimization
Database optimization
EsraaAlattar1
 
SQL Server 2008 Portfolio
SQL Server 2008 PortfolioSQL Server 2008 Portfolio
SQL Server 2008 Portfolio
lilredlokita
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
Tony Wong
 
May Woo Bi Portfolio
May Woo Bi PortfolioMay Woo Bi Portfolio
May Woo Bi Portfoliomaywoo
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
Dhananjay Goel
 
Data warehousing
Data warehousingData warehousing
Data warehousing
Ashish Kumar Jena
 
Sql General
Sql General Sql General
Sql General
Praveen Tiwari
 
Sql basics
Sql basicsSql basics
Sql basicsKumar
 

Similar to Practical guide to SQL basics (20)

Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
AimTo give you practical experience in database modelling, no.docx
AimTo give you practical experience in database modelling, no.docxAimTo give you practical experience in database modelling, no.docx
AimTo give you practical experience in database modelling, no.docx
 
Intro To TSQL - Unit 4
Intro To TSQL - Unit 4Intro To TSQL - Unit 4
Intro To TSQL - Unit 4
 
IT301-Datawarehousing (1) and its sub topics.pptx
IT301-Datawarehousing (1) and its sub topics.pptxIT301-Datawarehousing (1) and its sub topics.pptx
IT301-Datawarehousing (1) and its sub topics.pptx
 
Iowa liquor sales
Iowa liquor salesIowa liquor sales
Iowa liquor sales
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
Warranty management system
Warranty management systemWarranty management system
Warranty management system
 
SQL Select Distinct Statement
SQL Select Distinct StatementSQL Select Distinct Statement
SQL Select Distinct Statement
 
Lamar University - MISY 3310 Access ProjectPremiere Products.docx
Lamar University - MISY 3310 Access ProjectPremiere Products.docxLamar University - MISY 3310 Access ProjectPremiere Products.docx
Lamar University - MISY 3310 Access ProjectPremiere Products.docx
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and Apex
 
Physical data modeling
Physical data modelingPhysical data modeling
Physical data modeling
 
Database optimization
Database optimizationDatabase optimization
Database optimization
 
SQL Server 2008 Portfolio
SQL Server 2008 PortfolioSQL Server 2008 Portfolio
SQL Server 2008 Portfolio
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
May Woo Bi Portfolio
May Woo Bi PortfolioMay Woo Bi Portfolio
May Woo Bi Portfolio
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Data warehousing
Data warehousingData warehousing
Data warehousing
 
Sql General
Sql General Sql General
Sql General
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql basics
Sql basicsSql basics
Sql basics
 

Recently uploaded

哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
javier ramirez
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
mzpolocfi
 
Nanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdfNanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdf
eddie19851
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
dwreak4tg
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
sameer shah
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
GetInData
 
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdfUnleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Enterprise Wired
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
roli9797
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
Walaa Eldin Moustafa
 

Recently uploaded (20)

哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
 
Nanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdfNanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdf
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
 
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdfUnleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
 

Practical guide to SQL basics

  • 1. 1
  • 2. ABCCompaniesLimited CUSTOMERS: Name, Age, Gender, Email Id, Loyalty program member, etc EMPLOYEES: Name, Age, Gender, Email Id, Title, Pay Scale, Hire date, etc SUPPLIERS: Name, Address, Contact, Product ID, Price, Units, etc SHAREHOLDERS: Name, Number of shares owned, percentage of total equity, etc CREDITORS: Name, type, Days, etc PRODUCTS: Id, Category, MRP, Units, etc STORES: ID, Location, Type, Area, etc SALES: Current Year, Previous Year, YOY, YTD, etc Let us consider a large retailer named ABC Companies limited. The data for such a retailer would majorly be about its customers, employees, suppliers, shareholders, creditors, products, stores and sales as shown: These facts, ex: Name-Vin, Age-26, Units-40, Price-$34.95, etc, or in other words, these set of values with respect to the variables is Data. What is Data? 2
  • 3. Organized Data A few product Id’s and Units at a particular store of ABC Companies Ltd is as follows: FD139, 250, FD760 , 375, JH565, 410, KU206, 190, SY408, 200, BK213, 255, FR908, 384, SL650, 293, ZM987, 212, LK209, 368 This form of representation of data becomes confusing when the number of products increase. Representation of data in a tabular form as shown is easy to interpret and analyse the data. Product Id Units FD139 250 FD760 375 JH565 410 KU206 190 SY408 200 BK213 255 FR908 384 SL650 293 ZM987 212 LK209 368 Suppose we have data for 1000 product Ids and the company wants to know the product Ids having more than 250 units. Unorganized data: Will take a lot of time and resource to gather the required information Organized data in ascending order of units: Will be very quick and easy to sort out the required information. 3
  • 4. Database STORE Store_ID Store_Code Store_Name Store_Region_ID Store_Type STORE BILLING Bill_Number Customer_ID EmployeeID Store_Billing_Date Store_Billing_Time Store_Start_Date Store_ID PRODUCTS ProductID ProductName SupplierID CategoryID Product Code Product Country BrandID More… BRAND BrandID CategoryID Brand_Description STORE REGION Store_Region_ID Store_Region_Nmae Store_County Store_State DEPARTMENT Emp_Department_ID Department Name Department Code Department Description Department Head Department_Start_Date EMPLOYEES EmployeeID Emp_Department_ID Manager_ID LastName FirstName Title DOB HireDate SUPPLIER_TYPE Supplier_Type_ID Supplier_Type Supplier_Description SUPPLIERS SupplierID Supplier_Type_ID Supplier_Name Supplier_Location_Manager Supplier_Region STORE BILLING DETAILS Bill_Number ProductID UnitPrice Quantity Total Amount Discount Storage cost per item Cost price per item Labour cost per item PACKAGING PackageID Package_Description Package_Type CATEGORIES CategoryID CategoryName Description Picture REGION RegionID Customers_Region_Description CUSTOMERS CustomerID Customer_Type_ID Customer_Region_ID Customer_Shopper_ID Customer_Full_Name Customer_First_Name Customer_Last_Name Address City Region TERRITORIES TerritoryID Customer_Territory_Description RegionID CUSTOMER TYPE Customer_Type_ID Customer_Type Customer_Description Database can be considered as a cupboard of organized data. Each drawer of the cupboard represents a table also called an Entity. Ex: Employees, Products, Packaging, etc Each Entity has variables known as Fields. Ex: EmployeeID, Title, DOB, etc are the fields in the Employees Entity. Each field has values known as Data. Ex: BrandID CategoryID 162 SD125 208 GF326 363 FF118 284 DP251 189 RM318 DATA 4
  • 5. RELATIONAL DATABASE Each Table/Entity is a Relation as it contains related data. Suppose I need a list of Customers with their ID, Type and store billing details. • In order to get the customer type, there needs to be a relation between Customers and Customer type which is established based on a common field which is ‘Customer_Type_ID’. • Similarly, to get the store billing details, there should be a relation between Customers, Store billing and Store billing details based on a common field which is ‘CustomerID’ and ‘Bill_Number’ respectively. A database which lets us look at any of the entities and allows us to derive more information through its relation with other entities is known as a Relational Database. Note: Fields in Bold are Primary Key 5
  • 6. Relational Database Management System • RDBMS is a software that can be considered as a gatekeeper to relational database. • It provides users and programmers with a systematic way to create, retrieve, update and delete data in a database. • It ensures that data is consistently organized and remains easily accessible. Relational Database RDBMS User ABC Companies Limited App 6
  • 7. STRUCTURED QUERY LANGUAGE (SQL) The language used to interact with a relational database is known as Structured Query Language (SQL). The commands in SQL are classified in three groups as shown Suppose we want to create a database called ‘ABCretail’. We can use the following commands to create, see and select the database respectively. SQLCommands Data Definition Language (DDL) Data Manipulation Language (DML) Data Control Language (DCL) CREATE ALTER DROP SELECT UPDATE INSERT DELETE GRANT REVOKE Create database ABCretail; >>This will create a database called ‘ABCretail’ Use ABCretail; >>This will select ‘ABCretail’ as the database for data retrieval. Drop database ABCretail; >> This will delete ‘ABCretail’ database. *SQL Commands in blue 7
  • 8. Data Definition Language CREATE TABLE command creates an entity structure in the database by defining the following: • Entity Name: SQL throws error for names with space character. Putting the name within square brackets [ ] works well for names with space character. • Field Name: Putting the name within square brackets [ ] works well for names with space character. • Field data type: The type of data that shall be going within the Column is defined. • Primary key: It is a field which uniquely identidies each record. It can not have Null values. There can be only one primary key in a table which can be either on one field or more than one field. Basic Syntax: Create table table name (field1 datatype, field2 datatype, field3 datatype, .....fieldN datatype, PRIMARY KEY (one or more columns )); 8
  • 9. Ex: Let us create the following entities: Create table Suppliers (SupplierID int primary key not null, Supplier_Type_ID int, Supplier_Name varchar(100), Supplier_Location_Manager varchar(100), Supplier_Region varchar(75)); Create table [Store Region] (Store_Region_ID int primary key not null, Store_Region_Name varchar(100), Store_County varchar(50), Store_State varchar(100)); Create table Store (Store_ID int primary key not null, Store_Code int, Store_Name varchar (75), Store_Region_ID int references [Store Region] (Store_Region_ID), Store_Type varchar (50)); • The relation between Store Region and Store is based on a common field ‘Store_Region_ID’. • It acts as a cross-reference between tables because it references the primary key of another table which in this case is Store Region, thereby establishing a link between them. • Such a common field providing link between two tables is known as Foreign key. Data Definition Language [Cont..] SUPPLIERS SupplierID Supplier_Type_ID Supplier_Name Supplier_Location_Manager Supplier_Region STORE REGION Store_Region_ID Store_Region_Name Store_County Store_State STORE Store_ID Store_Code Store_Name Store_Region_ID Store_Type 9
  • 10. Data Definition Language [Cont..] ALTER TABLE command is used to add, delete or modify fields in an existing entity. • Let us add a field ‘Supplier_Contact_info’ to the Suppliers entity. Alter table Suppliers add Supplier_Contact_Info varchar(50); • Drop field ‘Store_County’ from Store Region. Alter table [Store Region] drop Store_County; • Let us modify the datatype of Store_ID from int (as defined in the previous slide) to smallint. Alter table Store modify column Store_ID smallint; DROP TABLE command removes the table definition and all the data for that table. Once a table is deleted then all the information available in that table will also be lost forever. • Suppose we want to drop Store Region Drop table [Store Region]; 10
  • 11. Data MANIPULATION Language Consider the following entities populated with data as shown below: SUPPLIERS SupplierID Supplier_Type_ID Supplier_Name Supplier_Location _Manager Supplier_Region 3589 58 Zai Inc David Atlantic 5871 79 Johr services Chris Ontario 8746 34 Imi Labs Bob Quebec 4812 15 Yirtel Inc Angela Ontario 9578 46 Voda services Sara Quebec STORE Store_ID Store_Code Store_Name Store_Region_ID Store_Type 1547 15 Monezis 258 Speciality 2624 71 Campust 358 Super market 5455 89 Leastprice 879 Discount 3879 69 Bacabus 157 Super market 6894 37 Medicas 451 Drug store STORE REGION Store_Region_ID Store_Region_Name Store_County Store_State 451 Atlantic Halifax Nova Scotia 879 Quebec Montreal Quebec 358 Ontario Peterborough Ontario 157 Atlantic Charlottetown Prince Edward Island 258 Ontario Simcoe Ontario *1 *2 *3 *4 *5 a 11
  • 12. Data MANIPULATION Language [CONT..] SELECT command is used to retrieve data from database entities. Select SupplierID, Supplier_Type_ID, Supplier_Name from Suppliers >> This retrieves all the records for fields SupplierID, Supplier_Type_ID and Supplier_Name from Suppliers entity.*1 Select * from Suppliers >> This gets all the records for all the fields in the entity. The result is the whole suppliers entity as shown in previous slide. Select top 2 * from [Store Region] >> Returns a (Refer slide 13) SELECT command with conditional statements using WHERE, LIKE, AND, OR clauses. Select * from Suppliers where SupplierID = 8746 >> Returns *2 Select Store_Region_ID, Store_Region_Name from [Store Region] where Store_County = ‘Peterborough’ >> Returns *3 Select * from Store where Store_Name like ‘M%’ and Store_Code = 15 >> Looks for records where Store_Name starts with ‘M’ and Store_Code is 15 and returns *4 Select * from Store where Store_Type = ‘Super market’ or Store_Code = 71 >> Looks for records where Store_Name starts with ‘M’ or Store_Code is 15 and returns *5 12Note: * refers to slide 11
  • 13. Data MANIPULATION Language [CONT..] SELECT command with conditional statements using WHERE, GROUP BY, HAVING, ORDER BY clauses. As per Store Billing Details, Suppose we need a list of unique bill_number with their respective sum of [Total Amount] with the following conditions: 1) UnitPrice > 16.00 2) Sum of [Total Amount] > 110 in descending order of Bill_Number. Select Bill_Number, sum([Total Amount]) from [Store Billing Details] where UnitPrice > 16 group by Bill_Number having sum([Total Amount]) > 110 order by Bill_Number Desc STORE BILLING DETAILS Bill_Number ProductID UnitPrice Quantity Total Amount Discount Storage cost per item Cost price per item Labour cost per item 12345 11 20.99 5 104.95 3% 0.75 14.50 1.50 67891 25 15.49 2 30.98 2% 1.00 9.90 1.50 01112 48 121.89 1 121.89 5% 5.00 60.50 15.00 12345 67 78.45 2 156.9 5% 2.0 50.00 5.00 51617 83 35.55 3 106.65 2% 2.50 15.00 4.00 01112 57 90.99 1 90.99 5% 2.50 65.49 10.00 Bill_Number Sum(Total Amount) 12345 261.85 01112 212.88 13
  • 14. Data MANIPULATION Language [CONT..] INSERT INTO command is used to add new rows of data to an entity in the database. Let us add 2 more rows of data to Store Billing Details as shown in previous slide Insert into [Store Billing Details] (Bill_Number, PoductID, UnitPrice, Quantity, [Total Amount], Discount, [Storage cost per item], [Cost price per item], [Labour cost per item]) values (25987, 35, 52.49, 3, 157.47, ‘5%’, 2.50, 35.50, 2.00), (65874, 88, 35.55, 3, 106.65, ‘2%’, 2.50, 16.00, 3.00) STORE BILLING DETAILS Bill_Number ProductID UnitPrice Quantity Total Amount Discount Storage cost per item Cost price per item Labour cost per item 12345 11 20.99 5 104.95 3% 0.75 14.50 1.50 67891 25 15.49 2 30.98 2% 1.00 9.90 1.50 01112 48 121.89 1 121.89 5% 5.00 60.50 15.00 12345 67 78.45 2 156.9 5% 2.00 50.00 5.00 51617 83 35.55 3 106.65 2% 2.50 15.00 4.00 01112 57 90.99 1 90.99 5% 2.50 65.49 10.00 25987 35 52.49 3 157.47 5% 2.50 35.50 2.00 65874 88 35.55 3 106.65 2% 2.50 16.00 3.00 14
  • 15. Data MANIPULATION Language [CONT..] UPDATE and DELETE commands are used to modify and delete the existing records in an entity respectively. 1) We want to change the discount percentage of the highlighted cell to 10%. 2) We want to keep only those records where quantity is less than 2. 3) Delete all records from table. 1) Update [Store Billing Details] set Discount = ’10%’ where Bill_Number = 01112 and ProductID = 48 2) Delete from [Store Billing Details] where Quantity < 2 3) Delete from [Store Billing Details] STORE BILLING DETAILS Bill_Number ProductID UnitPrice Quantity Total Amount Discount Storage cost per item Cost price per item Labour cost per item 12345 11 20.99 5 104.95 3% 0.75 14.50 1.50 67891 25 15.49 2 30.98 2% 1.00 9.90 1.50 01112 48 121.89 1 121.89 5% 5.00 60.50 15.00 12345 67 78.45 2 156.9 5% 2.00 50.00 5.00 51617 83 35.55 3 106.65 2% 2.50 15.00 4.00 01112 57 90.99 1 90.99 5% 2.50 65.49 10.00 25987 35 52.49 3 157.47 5% 2.50 35.50 2.00 65874 88 35.55 3 106.65 2% 2.50 16.00 3.00 STORE BILLING DETAILS Bill_Number ProductID UnitPrice Quantity Total Amount Discount Storage cost per item Cost price per item Labour cost per item 01112 48 121.89 1 121.89 5% 5.00 60.50 15.00 01112 57 90.99 1 90.99 5% 2.50 65.49 10.00 15
  • 16. Data Control Language Data control language commands are used by database administrator or owner of the database object to provide/remove privileges on a database object. GRANT command is used by database administrator to provide access or privileges on the database objects to the users. Grant select on Employees to user1 >> This command grants a SELECT permission on Employees entity to user1. REVOKE command removes user access rights or privileges to the database objects. Revoke select on Employees from user1 >> This command will revoke a SELECT privilege on employee entity from user1. 16
  • 17. JOINS In order to derive more information through entities relation with each other, we need to combine entities based on common fields using JOIN clause. From the above two entities, let’s say, we want to know all the store Id’s with their Name, type and state. Select Store_ID, Store_Name, Store_Type, Store_State from [Store Region] join Store on [Store Region].Store_Region_ID = Store. Store_Region_ID >> 17 STORE Store_ID Store_Code Store_Name Store_Region_ID Store_Type 1547 15 Monezis 258 Speciality 2624 71 Campust 358 Super market 5455 89 Leastprice 879 Discount 3879 69 Bacabus 157 Super market 6894 37 Medicas 451 Drug store 4426 25 Medicas 554 Drug store 7413 46 Leastprice 631 Discount STORE REGION Store_Region_ID Store_Region_Name Store_County Store_State 451 Atlantic Halifax Nova Scotia 879 Quebec Montreal Quebec 358 Ontario Peterborough Ontario 157 Atlantic Charlottetown Prince Edward Island 258 Ontario Simcoe Ontario Store_ID Store_Name Store_Type Store_State 1547 Monezis Speciality Ontario 2624 Campust Super market Ontario 5455 Leastprice Discount Quebec 3879 Bacabus Super market Prince Edward Island 6894 Medicas Drug store Nova Scotia This reflects the common field on which the join is based. In this case, Store_Region_ID is a common field where it is a primary key in Store Region and a foreign key in Store.
  • 18. Types of JOINS Inner Join: Returns only those set of records that match in both the entities. Inner Join is same as join, as explained in the previous slide. Left Join: Returns all rows from the left entity, even if there are no matches in the right entity. Select Store_ID, Store_State from Store left join [Store Region] on Store.Store_Region_ID = [Store Region].Store_Region_ID >> Right Join: Returns all rows from the right entity, even if there are no matches in the left entity. Select Store_ID, Store_State from Store right join [Store Region] on Store.Store_Region_ID = [Store Region].Store_Region_ID Full Join: Returns all the records from both the entities. It is a combination of the result of both left join and right join. Select Store_ID, Store_State from Store full join [Store Region] on Store.Store_Region_ID = [Store Region].Store_Region_ID 18 Store_ID Store_State 1547 Ontario 2624 Ontario 5455 Quebec 3879 Prince Edward Island 6894 Nova Scotia 4426 Null 7413 Null Store_ID Store_State 6894 Nova Scotia 5455 Quebec 2624 Ontario 3879 Prince Edward Island 1547 Ontario
  • 19. Why do we use SQL when we have Excel? • Considering the huge database of ABC Companies Limited (refer slide 4), with some entities like store billing going up to millions of records, Excel won’t last a few hours before it comes unusable. • Let’s say we want to know the number of units of a particular product broken down by store and how many of those customers are end users. In a SQL database, that’s a fairly easy query to write and we will retrieve data in seconds. Doing the same in excel might be arduous and time consuming. • SQL separates analysis from data. When using SQL, data is stored separately from analysis. That saves us from emailing large excel files. Instead send small text files having the instructions for analysis. Also saves from having to manage file versions or risk corrupting the data. 19
  • 20. APPENDIX DATATYPES 20 DATA TYPE FROM TO bigint -9,223,372,036,854,775,808 9,223,372,036,854,775,807 int -2,147,483,648 2,147,483,647 smallint -32,768 32,767 tinyint 0 255 bit 0 1 decimal -10^38+1 10^38-1 numeric -10^38+1 10^38-1 money -922,337,203,685,477.5808 +922,337,203,685,477.5807 smallmoney -214,748.3648 +214,748.3647 Exact Numeric Data Types
  • 21. APPENDIX DATATYPES 21 DATA TYPE Description char Maximum length of 8,000 characters.(Fixed length non-Unicode characters) varchar Maximum of 8,000 characters.(Variable length non-Unicode data) varchar(max) Maximum length of 231 characters. Variable length non-Unicode data (SQL Server 2005 only). text Variable length non-Unicode data with a maximum length of 2,147,483,647 characters. Character Strings Data Types DATA TYPE Description nchar Maximum length of 4,000 characters.(Fixed length Unicode) nvarchar Maximum of 4,000 characters.(Variable length Unicode) nvarchar(max) Maximum length of 231 characters. Variable length Unicode (SQL Server 2005 only). ntext Maximum length of 1,073,741,823 characters. (Variable length Unicode) Unicode Character Strings Data Types
  • 22. 22 All the commands (Highlighted in blue) work for MS SQL Server. Thank You Created by Pavani Ganti