8/10/2019 SQL Presentation(V1)
1/44
To protect the confidential and proprietary information included in this material, it may notbe disclosed or provided to any third parties without the approval of Hewitt Associates LLC.
SQL - an overview by Sridharan
8/10/2019 SQL Presentation(V1)
2/44
2[MM/DD/YYYY]
Agenda1. SQL In tr od uc tio n
2. SQL St at em en ts
3. B as ic s yn tax
4. Sel ec t St at em en ts
5. D at a f r om M u lt ip l e Ta bl e
6. Set o per ato rs
7. SQL Fu nc tio n
8. SQL Su b q uer ies
9. SQL View s
1 0. In c l u d in g C o n s tr ai n t s
11. Q u es t io n s
8/10/2019 SQL Presentation(V1)
3/44
3[MM/DD/YYYY]
Introduction to SQL
8/10/2019 SQL Presentation(V1)
4/44
4[MM/DD/YYYY]
Introduction to SQL
SQL stands for Structured Query Language.
AIM : In this session you will learn how to use SQL to access and manipulate data in MySQL, SQL Server, MS Access,Oracle, Sybase, DB2, and other database systems.
What is SQL?SQL is a standard language for accessing and manipulating databases.
When a user wants to get some information from a database file, he can issue a query.
A query is a user request to retrieve data or information with a certain condition.SQL is a query language that allows user to specify the conditions. (instead of algorithms -http://csis.pace.edu/~wolf/CIS101/algorithms1/sld001.htm ).
Concept of SQLThe user specifies a certain condition.
The program will go through all the records in the database file and select those records that satisfy the condition.(searching).
Statistical information of the data.
The result of the query will then be stored in form of a table.
8/10/2019 SQL Presentation(V1)
5/44
5[MM/DD/YYYY]
SQL Statements
8/10/2019 SQL Presentation(V1)
6/44
6[MM/DD/YYYY]
SQL Statements
Select - Data Retrival
InsertUpdate
Delete - Data Manipulation
Merge
Create
Alter
Drop -
Data Definition LanguageRename
Truncate
Commit
RollBack - Transaction Control
Savepoint
Grant
Revoke - Data control Language
8/10/2019 SQL Presentation(V1)
7/44
7[MM/DD/YYYY]
SQL Syntax
8/10/2019 SQL Presentation(V1)
8/44
8[MM/DD/YYYY]
SQL Syntax
8/10/2019 SQL Presentation(V1)
9/44
9[MM/DD/YYYY]
SQL Syntax (contd..)
8/10/2019 SQL Presentation(V1)
10/44
10[MM/DD/YYYY]
SQL Syntax (contd..)
8/10/2019 SQL Presentation(V1)
11/44
11[MM/DD/YYYY]
SQL Syntax (contd..)
8/10/2019 SQL Presentation(V1)
12/44
12[MM/DD/YYYY]
Select statement
8/10/2019 SQL Presentation(V1)
13/44
13[MM/DD/YYYY]
Select Statement
1. The SQL SELECT Statement
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
SELECT ...... FROM ...... WHERE ......
1. Select Syntax
SELECT [ALL / DISTINCT] expr1 [AS col1 ], expr2 [AS col2 ] ;
FROM tablename WHERE condition
8/10/2019 SQL Presentation(V1)
14/44
14[MM/DD/YYYY]
Data from Multiple Table
8/10/2019 SQL Presentation(V1)
15/44
15[MM/DD/YYYY]
Data from Multiple Table
SQL provides a convenient operation to retrieve information from multiple tables.
This operation is called join.
The join operation will combine the tables into one large table with all possible combinations
(Math: Cartesian Product), and then it will filter the rows of this combined table to yield useful information.
field1
A
B
field2
1
2
3
field1 field2
AA
A
12
3
B
BB
1
23
8/10/2019 SQL Presentation(V1)
16/44
16[MM/DD/YYYY]
Data from Multiple Table (Contd..)
Types of Joins
Equijoin
To determine an employees department name, you compare the value in the department_ID column in the employees tablewith the department_id values in the departments tables. The relationship between the employees and departments tablesin an equijoin,that is ,values in the department id column on both must be equal .frequently, this type of join involves primaryand foreign key complements. ( Note : Equijoins are also called simple joins or inner joins.)
Non equijoin
A none equijoin is a join condition containing something other then an equality operator.
8/10/2019 SQL Presentation(V1)
17/44
17[MM/DD/YYYY]
Data from Multiple Table (Contd..)
Outer join
Missing rows can be returned If an outer join operator is used in the join condition .the operation is plus sign enclosedin parentheses(+), and it is placed on the side of the join that is deficient in information. This operator has the effect ofcreating one or more null rows, to which one or more rows from the nondeficient table can be joined.
Two types of outer join.
1. Right outer join
2. Left outer join
Self join
Sometimes you need to join a table itself. To find the name of each employees, you need to join the employees tableto itself. Or perform a self join. For example ,to find the name of sridhars manager ,you need to :
1. Find sridhar in the employees table by looking at the last name column.
2. Find the manager number for sridhar by looking at the manager_ID column .when sridhars manager is 101 .
3. Find the name of the manager with employee_ID 101 by looking at the last name column.
Narasimhan employee number is 101 .so Narasimhan is sridhars manager. In this process you look in the table twice.
8/10/2019 SQL Presentation(V1)
18/44
18[MM/DD/YYYY]
Set operators
8/10/2019 SQL Presentation(V1)
19/44
19[MM/DD/YYYY]
Set operators
The set operators
The set operators combine the results of two or more component queries into one result. Queries containing setoperators are called compound queries.
Types of operators
1. UNION
2. UNION ALL
3. INTERSECT4. MINUS
8/10/2019 SQL Presentation(V1)
20/44
20[MM/DD/YYYY]
Set operators (Contd..)
8/10/2019 SQL Presentation(V1)
21/44
21[MM/DD/YYYY]
Set operators (Contd..)
UNION
All distinct rows selected by either query
UNION ALL
All rows selected by either query. including all duplicates.
INTERSECT
All distinct rows selected by both queries.
MINUS
All distinct rows that are selected by the first select statement and that are not selected in the second select statement .
8/10/2019 SQL Presentation(V1)
22/44
22[MM/DD/YYYY]
Function
8/10/2019 SQL Presentation(V1)
23/44
23[MM/DD/YYYY]
SQL Function
Functions are a very powerful feature of SQL and can be used to do the following ;
1. Perform calculation on data
2. Modify individual data items
3. Manipulate output for groups of rows
4. Format dates and numbers for display
5. Convert column data types
6. SQL functions sometimes take arguments and always return a value.
8/10/2019 SQL Presentation(V1)
24/44
24[MM/DD/YYYY]
Two types of SQL Function
Function
Single row function Multiple-row function
8/10/2019 SQL Presentation(V1)
25/44
25[MM/DD/YYYY]
Single Row Function
These functions operate on single rowsand return one result per row.
Character
Number
Date
Conversion
Multi-Row Function
Functions can Manipulate groups ofRows.These functions are known asgroup functions.
Aggregate function
8/10/2019 SQL Presentation(V1)
26/44
26[MM/DD/YYYY]
Single Row Function
Character
Number
DateConversion
General
8/10/2019 SQL Presentation(V1)
27/44
27[MM/DD/YYYY]
Single Row Function
General Function
1. NVL
2. NVL2
3. NULLIF
4. COALSECE
5. CASE
6. DECODE
Character Function
Case Manipulation
1. UPPER
2. LOWER
3. INITCAP
Character Manipulation
1. CONCAT
2. SUBSTR3. LENTH
4. INSTR
5. LPAD | RPAD
6. TRIM
8/10/2019 SQL Presentation(V1)
28/44
28[MM/DD/YYYY]
Single Row Function
Number Function
1. ROUND
2. TRUNC
3. MOD
Date Function
1. MONTHS_BETWEEN
2. ADD_MONTHS
3. NEXT_DAY
4. LAST_DAY
5. ROUND
6. TRUNC
8/10/2019 SQL Presentation(V1)
29/44
29[MM/DD/YYYY]
Conversion Function
Data-TypeConversion
Implicit Data-TypeConversion
Explicit Data-TypeConversion
8/10/2019 SQL Presentation(V1)
30/44
30[MM/DD/YYYY]
Implicit Data-Type Conversion
FROM TO
VARCHAR2 OR CHAR NUMBER
VARCHAR2 OR CHAR DATE
NUMBER VARCHAR2
DATE VARCHAR2
8/10/2019 SQL Presentation(V1)
31/44
31[MM/DD/YYYY]
Explicit Data Type Conversion
CHARACTERNUMBER DATE
8/10/2019 SQL Presentation(V1)
32/44
32[MM/DD/YYYY]
Explicit Data Type Conversion (Continued)
Function Purpose
TO_CHAR Specifies the Langue in which month andday names and abbreviations arereturned.if this parameter is omitted,thisfunction uses the default date languages
for the session.TO_NUMBER Converts a character string containing
digits to a number in the format specifiedby the optional format model fmt.
TO_DATE Converts a character string representinga date to a date value according to theformat specified. If format is omitted, theformat is DD-MON-YY
8/10/2019 SQL Presentation(V1)
33/44
33[MM/DD/YYYY]
Multi-Row Function
Aggregate Functions: Aggregate functions operate against a collection of
values, but return a single value.
( Note: If used among many other expressions in the item list of a SELECTstatement, the SELECT must have a GROUP BY clause!)
8/10/2019 SQL Presentation(V1)
34/44
34[MM/DD/YYYY]
Aggregate Functions
Aggregate functions:Aggregate Row Description
AVG (column) Returns the average value of a column
COUNT (column)
Returns the number of rows (without a NULL value) of a column
COUNT (*) Returns the number of selected rows
COUNT (DISTINCT column) Returns the number of distinct results
MAX (column) Returns the highest value of a column
MIN (column) Returns the lowest value of a column
SUM (column) Returns the total sum of a column
8/10/2019 SQL Presentation(V1)
35/44
35[MM/DD/YYYY]
Subquery
8/10/2019 SQL Presentation(V1)
36/44
36[MM/DD/YYYY]
Subquery
Using a subquery to solve a problem
Who has a salary greater than kapil?
Main Query
Which employees have salaries greater than kapils salary?
Subquery
What is Kapils salary?
8/10/2019 SQL Presentation(V1)
37/44
37[MM/DD/YYYY]
Subquery (Contd..)
Types of subquery
Single-Row-Subquery
Main query
Subquery ----------> One row
Multiple Row Subquery
Main query Subquery --------> More than one row
8/10/2019 SQL Presentation(V1)
38/44
38[MM/DD/YYYY]
Views
8/10/2019 SQL Presentation(V1)
39/44
39[MM/DD/YYYY]
View (Contd..)
What is view?
You can present logical subset or combinations of data creating views of data. A view is logical table based ontable or another view. View contains no data of its own but is like a window through which data from tables can be viewed orchanged. The tables on which is a view is based are called base tables. The view is stored as a select statement in the datadictionary.
Why use views?To restrict data access.
To make complex query easy.
To provide data independence.
To present different views of the same data .
8/10/2019 SQL Presentation(V1)
40/44
40[MM/DD/YYYY]
View (Contd..)
Types of views:
1. Simple views2. Complex views
Simple views & Complex views
Feature Simple views Complex views
Number of tables. One One or More
Contains functions. No Yes
Contain groups of data. NO Yes
DML operations through a view. Yes Not always
8/10/2019 SQL Presentation(V1)
41/44
41[MM/DD/YYYY]
Constraints
8/10/2019 SQL Presentation(V1)
42/44
42[MM/DD/YYYY]
Constraints (Contd..)
What are constraints?
1. Constraints enforce rules at the table level.
2. Constraints prevent the deletion of a table if there are dependencies.
Types of constraints:1. NOT NULL
2. UNIQUE3. PRIMARY KEY
4. FORIGN KEY
5. CHECK
8/10/2019 SQL Presentation(V1)
43/44
43[MM/DD/YYYY]
Constraints (Contd..)
Data integrity constraints:
Constraint Description
NOT NULL Specifies that column value can not contain a null value.
UNIQUE Specifies a column or combination of columns whose value
must be unique for all rows in the table.
PRIMARY KEY Uniquely identifies each row of the table .
FOREIGN KEY Establishes and enforces a foreign key relationshipbetween the column and a column of the referenced table.
CHECK Specify a condition must be true.
8/10/2019 SQL Presentation(V1)
44/44
44[MM/DD/YYYY]
THANK YOU