Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Basics of SQL: by in Cma Team

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

Basics of SQL

BY IN CMA TEAM
Ever wondered how the CMA team is
able to give numbers in a jiff?
Usual Gyan
SQL Structured Query Language
Communicator with Database
Basic use Upload/retrieve Data from the Database
A basic SQL query consist of a SELECT, a FROM and a WHERE clause
Topics:
SQL Query Structure
Conditional Statements
Group By Statements
Date Functions
Joins
Set Operations
Other Functions
SQL query structure
SELECT
- Specifies the columns that appear in the output
FROM
- Specifies the tables from which the data is queried

WHERE
- Helps the user to get snapshot of data

GROUP BY
- Helps the user to get a summary of data
Conditional Statements
AND, OR, BETWEEN, LIKE, IN, NOT
ICC Team Ranking Sample Table:
Find Teams having matches > 20
Rank Team Matches Points Rating
and rating > 100
1 India 26 3,235 124
2 New Zealand 25 2,964 119
3 West Indies 19 2,249 118
4 South Africa 30 3,489 116
5 Australia 23 2,604 113
6 England 22 2,462 112
7 Pakistan 34 3,751 110
8 Sri Lanka 26 2,834 109
9 Afghanistan 25 2,010 80
10 Bangladesh 23 1,755 76
11 Scotland 17 1,005 59
12 Netherlands 18 1,054 59
13 Zimbabwe 25 1,429 57
14 Hong Kong 19 832 44
15 United Arab Emirates 17 730 43
16 Ireland 17 669 39
17 Oman 12 442 37
18 Papua New Guinea 6 51
ICC Team Ranking Sample Table:
Find Teams having matches > 20
Rank Team Matches Points Rating
and rating > 100
1 India 26 3,235 124
2 New Zealand 25 2,964 119 SELECT
3 West Indies 19 2,249 118 Team
4 South Africa 30 3,489 116 FROM
5 Australia 23 2,604 113 ICC_T20
6 England 22 2,462 112 WHERE
7 Pakistan 34 3,751 110 Matches > 20 and rating > 100
8 Sri Lanka 26 2,834 109
9 Afghanistan 25 2,010 80
10 Bangladesh 23 1,755 76
11 Scotland 17 1,005 59
12 Netherlands 18 1,054 59
13 Zimbabwe 25 1,429 57
14 Hong Kong 19 832 44
15 United Arab Emirates 17 730 43
16 Ireland 17 669 39
17 Oman 12 442 37
18 Papua New Guinea 6 51
ICC Team Ranking Sample Table:
Find Teams having matches > 20
Rank Team Matches Points Rating
and rating > 100
1 India 26 3,235 124
2 New Zealand 25 2,964 119 SELECT
3 West Indies 19 2,249 118 Team
4 South Africa 30 3,489 116 FROM
5 Australia 23 2,604 113 ICC_T20
6 England 22 2,462 112 WHERE
7 Pakistan 34 3,751 110 Matches > 20 and rating > 100
8 Sri Lanka 26 2,834 109
9 Afghanistan 25 2,010 80
10 Bangladesh 23 1,755 76
Find Teams having matches
11 Scotland 17 1,005 59
between 20 and 30
12 Netherlands 18 1,054 59
13 Zimbabwe 25 1,429 57
14 Hong Kong 19 832 44
15 United Arab Emirates 17 730 43
16 Ireland 17 669 39
17 Oman 12 442 37
18 Papua New Guinea 6 51
ICC Team Ranking Sample Table:
Find Teams having matches > 20
Rank Team Matches Points Rating
and rating > 100
1 India 26 3,235 124
2 New Zealand 25 2,964 119 SELECT
3 West Indies 19 2,249 118 Team
4 South Africa 30 3,489 116 FROM
5 Australia 23 2,604 113 ICC_T20
6 England 22 2,462 112 WHERE
7 Pakistan 34 3,751 110 Matches > 20 and rating > 100 ;
8 Sri Lanka 26 2,834 109
9 Afghanistan 25 2,010 80
10 Bangladesh 23 1,755 76
Find Teams having matches
11 Scotland 17 1,005 59
between 20 and 30
12 Netherlands 18 1,054 59
13 Zimbabwe 25 1,429 57 SELECT
14 Hong Kong 19 832 44 Team
15 United Arab Emirates 17 730 43 FROM
16 Ireland 17 669 39 ICC_T20
17 Oman 12 442 37 WHERE
18 Papua New Guinea 6 51 Matches between 20 and 30 ;
ICC Team Ranking Sample Table:
Find Teams Rating whose names
Rank Team Matches Points Rating
start with S or A
1 India 26 3,235 124
2 New Zealand 25 2,964 119
3 West Indies 19 2,249 118
4 South Africa 30 3,489 116
5 Australia 23 2,604 113
6 England 22 2,462 112
7 Pakistan 34 3,751 110
8 Sri Lanka 26 2,834 109
9 Afghanistan 25 2,010 80
10 Bangladesh 23 1,755 76
11 Scotland 17 1,005 59
12 Netherlands 18 1,054 59
13 Zimbabwe 25 1,429 57
14 Hong Kong 19 832 44
15 United Arab Emirates 17 730 43
16 Ireland 17 669 39
17 Oman 12 442 37
18 Papua New Guinea 6 51
ICC Team Ranking Sample Table:
Find Teams Rating whose names
Rank Team Matches Points Rating
start with S or A
1 India 26 3,235 124
2 New Zealand 25 2,964 119 SELECT
3 West Indies 19 2,249 118 Team Wildcard
4 South Africa 30 3,489 116 FROM Characters
5 Australia 23 2,604 113 ICC_T20
6 England 22 2,462 112 WHERE
7 Pakistan 34 3,751 110 (Lower(team) like a% or
8 Sri Lanka 26 2,834 109 lower(team) like s%) ;
9 Afghanistan 25 2,010 80
10 Bangladesh 23 1,755 76
11 Scotland 17 1,005 59
12 Netherlands 18 1,054 59
13 Zimbabwe 25 1,429 57
14 Hong Kong 19 832 44
15 United Arab Emirates 17 730 43
16 Ireland 17 669 39
17 Oman 12 442 37
18 Papua New Guinea 6 51
ICC Team Ranking Sample Table:
Find Teams Rating whose names
Rank Team Matches Points Rating
start with S or A
1 India 26 3,235 124
2 New Zealand 25 2,964 119 SELECT
3 West Indies 19 2,249 118 Team Wildcard
4 South Africa 30 3,489 116 FROM Characters
5 Australia 23 2,604 113 ICC_T20
6 England 22 2,462 112 WHERE
7 Pakistan 34 3,751 110 (Lower(team) like a% or
8 Sri Lanka 26 2,834 109 lower(team) like s%) ;
9 Afghanistan 25 2,010 80
10 Bangladesh 23 1,755 76
11 Scotland 17 1,005 59
Show Ratings of India and
12 Netherlands 18 1,054 59
Pakistan
13 Zimbabwe 25 1,429 57
14 Hong Kong 19 832 44
15 United Arab Emirates 17 730 43
16 Ireland 17 669 39
17 Oman 12 442 37
18 Papua New Guinea 6 51
ICC Team Ranking Sample Table:
Find Teams Rating whose names
Rank Team Matches Points Rating
start with S or A
1 India 26 3,235 124
2 New Zealand 25 2,964 119 SELECT
3 West Indies 19 2,249 118 Team Wildcard
4 South Africa 30 3,489 116 FROM Characters
5 Australia 23 2,604 113 ICC_T20
6 England 22 2,462 112 WHERE
7 Pakistan 34 3,751 110 (Lower(team) like a% or
8 Sri Lanka 26 2,834 109 lower(team) like s%) ;
9 Afghanistan 25 2,010 80
10 Bangladesh 23 1,755 76
11 Scotland 17 1,005 59
Show Ratings of India and
12 Netherlands 18 1,054 59
Pakistan
13 Zimbabwe 25 1,429 57 SELECT
14 Hong Kong 19 832 44 Team
,Rating Strings should be
15 United Arab Emirates 17 730 43
mentioned within
16 Ireland 17 669 39 FROM
single quotes
17 Oman 12 442 37 ICC_T20
18 Papua New Guinea 6 51 WHERE
Upper (team) in (INDIA, PAKISTAN) ;
ICC Team Ranking Sample Table:
Find the Team with No Ranking and replace
Rank Team Matches Points Rating
the Null Ranking in the output with -1
1 India 26 3,235 124
2 New Zealand 25 2,964 119
3 West Indies 19 2,249 118
4 South Africa 30 3,489 116
5 Australia 23 2,604 113
6 England 22 2,462 112
7 Pakistan 34 3,751 110
8 Sri Lanka 26 2,834 109
9 Afghanistan 25 2,010 80
10 Bangladesh 23 1,755 76
11 Scotland 17 1,005 59
12 Netherlands 18 1,054 59
13 Zimbabwe 25 1,429 57
14 Hong Kong 19 832 44
15 United Arab Emirates 17 730 43
16 Ireland 17 669 39
17 Oman 12 442 37
18 Papua New Guinea 6 51
ICC Team Ranking Sample Table:
Find the Team with No Ranking and replace
Rank Team Matches Points Rating
the Null Ranking in the output with -1
1 India 26 3,235 124
2 New Zealand 25 2,964 119 SELECT
3 West Indies 19 2,249 118 Team,
4 South Africa 30 3,489 116 NVL(Rating,-1) as Rating
5 Australia 23 2,604 113 FROM
6 England 22 2,462 112 ICC_T20
7 Pakistan 34 3,751 110 WHERE
8 Sri Lanka 26 2,834 109 Rating is NULL ;
9 Afghanistan 25 2,010 80
10 Bangladesh 23 1,755 76
11 Scotland 17 1,005 59
12 Netherlands 18 1,054 59
13 Zimbabwe 25 1,429 57
14 Hong Kong 19 832 44
15 United Arab Emirates 17 730 43
16 Ireland 17 669 39
17 Oman 12 442 37
18 Papua New Guinea 6 51
GROUP BY STATEMENT
AGGREGATING FUNCTIONS MIN, MAX, COUNT, DISTINCT, SUM
ETC.
ICC T20 Batsmen Ranking Sample Table:
How many nations figure in the Top 10
Pos Name Rating H_Rating H_Rating_Date Nation

1 Aaron Finch 841 892 2014/04/01 Australia


SELECT
Count (Distinct Nation) as Nation
2 Virat Kohli 834 897 2014/09/07 India
FROM
3 Alex Hales 786 866 2014/09/07 England ICC_Batsman_Ranking ;
4 Francois du Plessis 777 843 2015/07/05 South Africa

5 Martin Guptill 741 792 2012/02/19 New Zealand

6 Kane Williamson 733 737 2016/01/17 New Zealand

7 Chris Gayle 703 830 2012/10/05 West Indies

8 Mohammad Shahzad 682 682 2016/03/12 Pakistan

9 Hamilton Masakadza 677 699 2016/02/16 Zimbabwe

10 David Warner 675 824 2010/05/11 Australia


ICC T20 Batsmen Ranking Sample Table:
How many nations figure in the Top 10
Pos Name Rating H_Rating H_Rating_Date Nation

1 Aaron Finch 841 892 2014/04/01 Australia


SELECT
Count (Distinct Nation) as Nation
2 Virat Kohli 834 897 2014/09/07 India
FROM
3 Alex Hales 786 866 2014/09/07 England ICC_Batsman_Ranking ;
4 Francois du Plessis 777 843 2015/07/05 South Africa
No of batsmen, best, worst & average ratings
5 Martin Guptill 741 792 2012/02/19 New Zealand
in the top 10 by nation of origin
6 Kane Williamson 733 737 2016/01/17 New Zealand

7 Chris Gayle 703 830 2012/10/05 West Indies

8 Mohammad Shahzad 682 682 2016/03/12 Pakistan

9 Hamilton Masakadza 677 699 2016/02/16 Zimbabwe

10 David Warner 675 824 2010/05/11 Australia


ICC T20 Batsmen Ranking Sample Table:
How many nations figure in the Top 10
Pos Name Rating H_Rating H_Rating_Date Nation

1 Aaron Finch 841 892 2014/04/01 Australia


SELECT
Count (Distinct Nation) as Nation
2 Virat Kohli 834 897 2014/09/07 India
FROM
3 Alex Hales 786 866 2014/09/07 England ICC_Batsman_Ranking ;
4 Francois du Plessis 777 843 2015/07/05 South Africa
No of batsmen, best, worst & average ratings
5 Martin Guptill 741 792 2012/02/19 New Zealand
in the top 10 by nation of origin
6 Kane Williamson 733 737 2016/01/17 New Zealand

7 Chris Gayle 703 830 2012/10/05 West Indies SELECT


8 Mohammad Shahzad 682 682 2016/03/12 Pakistan
Nation
, count( distinct Name ) as Name
9 Hamilton Masakadza 677 699 2016/02/16 Zimbabwe
, min (h_rating) as worst_rating
10 David Warner 675 824 2010/05/11 Australia
,max (h_rating) as Best_rating
,avg (h_rating) as average rating
FROM
ICC_Batsman_Ranking
GROUP BY By default, SQL will sort in
Nation ascending order. If
ORDER BY descending order needed use
2 desc
HAVING CLAUSE
WHERE VS HAVING
ICC T20 Batsmen Ranking Sample Table:
Average rating of batsmen for teams with
Pos Name Rating H_Rating H_Rating_Date Nation
more than two batsmen in Top 10
1 Aaron Finch 841 892 2014/04/01 Australia

2 Virat Kohli 834 897 2014/09/07 India

3 Alex Hales 786 866 2014/09/07 England

4 Francois du Plessis 777 843 2015/07/05 South Africa

5 Martin Guptill 741 792 2012/02/19 New Zealand

6 Kane Williamson 733 737 2016/01/17 New Zealand

7 Chris Gayle 703 830 2012/10/05 West Indies

8 Mohammad Shahzad 682 682 2016/03/12 Pakistan

9 Hamilton Masakadza 677 699 2016/02/16 Zimbabwe

10 David Warner 675 824 2010/05/11 Australia


ICC T20 Batsmen Ranking Sample Table:
Average rating of batsmen for teams with
Pos Name Rating H_Rating H_Rating_Date Nation
more than two batsmen in Top 10
1 Aaron Finch 841 892 2014/04/01 Australia
SELECT
2 Virat Kohli 834 897 2014/09/07 India
Nation
3 Alex Hales 786 866 2014/09/07 England ,avg(rating) as avg_rating
4 Francois du Plessis 777 843 2015/07/05 South Africa ,count(name) as players
5 Martin Guptill 741 792 2012/02/19 New Zealand
FROM
ICC_Batsman_Ranking
6 Kane Williamson 733 737 2016/01/17 New Zealand
GROUP BY
7 Chris Gayle 703 830 2012/10/05 West Indies Nation
8 Mohammad Shahzad 682 682 2016/03/12 Pakistan HAVING
9 Hamilton Masakadza 677 699 2016/02/16 Zimbabwe
Count(name) >= 2
;
10 David Warner 675 824 2010/05/11 Australia

difference between the having and where clause in SQL is that the where clause can
not be used with aggregates, but the having clause can.
Date Functions
TO_CHAR,TO_NUMBER,TO_DATE,TRUNC
Date functions help in manipulation and aggregation of data basis dates
TO_DATE() conversion of string to DATE
TO_CHAR() helps format dates to VARCHAR. Especially helpful in aggregating the data from daily to weekly,
monthly, quarterly or yearly level
TRUNC() - provides the first date of the period based on the argument.
Few examples

H_rating_date > = to_date(2016/03/10,YYYY/MM/DD)


To_char(h_rating_date,DD) as Day
To_char(h_rating_date,MM) as Month
To_char(h_rating_date,Q) as Month as Quarter
To_number(To_char(h_rating_date,YYYY)) as Year
Trunc(h_rating_date,MM) as month
Highlight the day, month, quarter, year in which the batsmen achieved their highest ever ranking sorted
by year (From New to Old) not older than 2012
Highlight the day, month, quarter, year in which the batsmen achieved their highest ever ranking sorted
by year (From New to Old) not older than 2012

SELECT
Name
, TO_CHAR(H_Rating_Date, DD ) as H_DAY
, TO_CHAR(H_Rating_Date, MM ) as H_MONTH
, TO_CHAR(H_Rating_Date, Q ) as H_QUARTER
, TO_NUMBER(TO_CHAR(H_Rating_Date, Q ) )as H_YEAR

FROM
ICC_Batsmen_Ranking
WHERE
H_Rating_Date > TO_DATE(2011/12/31,YYYY/MM/DD)

ORDER BY
5 DESC
;
Joins
INNER JOINS, OUTER JOINS, LEFT JOINS, RIGHT JOINS
Inner Join
Commonly used join
This join select records that have matching values in both
the tables
Inner join is same as join. The Keyword INNER is optional
SYNTAX :
SELECT a.col1, b.col2
FROM table1 a join table2 b
On a.col3= b.col3
OR
SELECT a.col1, b.col2
FROM table1 a, table2 b
WHERE a.col3 = b.col3
Left Join
Select records from the first (left-most) table
with matching right table records.
SYNTAX
SELECT a.col1, b.col2
FROM table1 a , table2 b
On a.col3= b.col3(+)
Right Join
Select records from the second (right-most)
table with matching left table records.
SYNTAX
SELECT a.col1, b.col2
FROM table1 a ,table2 b
WHERE a.col3(+)= b.col3
ICC Team Ranking Sample Table: ICC T20 Batsmen Ranking Sample Table:

Rank Team Matches Points Rating Pos Name Rating H_Rating H_Rating_Date Nation
1 India 26 3,235 124
1 Aaron Finch 841 892 2014/04/01 Australia
2 New Zealand 25 2,964 119
2 Virat Kohli 834 897 2014/09/07 India
3 West Indies 19 2,249 118
4 South Africa 30 3,489 116 3 Alex Hales 786 866 2014/09/07 England

5 Australia 23 2,604 113 4 Francois du Plessis 777 843 2015/07/05 South Africa
6 England 22 2,462 112
5 Martin Guptill 741 792 2012/02/19 New Zealand
7 Pakistan 34 3,751 110
6 Kane Williamson 733 737 2016/01/17 New Zealand
8 Sri Lanka 26 2,834 109
9 Afghanistan 25 2,010 80 7 Chris Gayle 703 830 2012/10/05 West Indies

10 Bangladesh 23 1,755 76 8 Mohammad Shahzad 682 682 2016/03/12 Pakistan


11 Scotland 17 1,005 59
9 Hamilton Masakadza 677 699 2016/02/16 Zimbabwe
12 Netherlands 18 1,054 59
10 David Warner 675 824 2010/05/11 Australia
13 Zimbabwe 25 1,429 57
14 Hong Kong 19 832 44
15 United Arab Emirates 17 730 43
16 Ireland 17 669 39
17 Oman 12 442 37
18 Papua New Guinea 6 51
How many batsmen figure in the top 6
for countries ranked within top 5?
How many batsmen figure in the top 6
for countries ranked within top 5?

SELECT
a.Team
,a.Rank
,count (Distinct B.Name) as Batsmen
FROM
ICC_T20 a, ICC_Batsmen_Ranking b
WHERE a.rank <= 5
And a.team = b.nation (+)
And b.pos(+) <= 6
GROUP BY
a.Team
,b.Rank
How many batsmen figure in the top 6 Select rank of only those countries whose
for countries ranked within top 5? batsmen figure in the top 6

SELECT /*+ USE_HASH( a, b )*/


a.Team
,a.Rank
,count (Distinct B.Name) as Batsmen
FROM
ICC_T20 a, ICC_Batsmen_Ranking b
WHERE a.rank <= 5
And a.team = b.nation (+)
And b.pos(+) <= 6
GROUP BY
a.Team
,b.Rank
How many batsmen figure in the top 6 Select rank of only those countries whose
for countries ranked within top 5? batsmen figure in the top 6

SELECT SELECT
a.Team a.Team
,a.Rank ,a.Rank
,count (Distinct B.Name) as Batsmen FROM
FROM ICC_T20 a, ICC_Batsmen_Ranking b
ICC_T20 a, ICC_Batsmen_Ranking b WHERE
WHERE a.rank <= 5 a.team = b.nation
And a.team = b.nation (+) And b.pos <= 6
And b.pos(+) <= 6 GROUP BY
GROUP BY a.Team
a.Team ,b.Rank
,b.Rank
SQL Operators
MINUS, INTERSECT, UNION, UNION ALL
UNION
combines the results of 2 tables & eliminates duplicate records

UNION ALL
combines the results of 2 tables and returns all rows. It does not eliminate duplicate records

INTERSECT
Returns records which are common in both the tables

MINUS
returns records of the first table which are not present in the second table
UNION UNION ALL

SELECT SELECT
Team as Nation Team as Nation
From From
ICC_T20 ICC_T20

UNION UNION ALL

SELECT SELECT
Nation Nation
FROM FROM
ICC_Batsmen_Ranking ICC_Batsmen_Ranking
; ;
INTERSECT MINUS

SELECT SELECT
Team as Nation Team as Nation
From From
ICC_T20 ICC_T20

INTERSECT MINUS

SELECT SELECT
Nation Nation
FROM FROM
ICC_Batsmen_Ranking ICC_Batsmen_Ranking
; ;
Decode & Case
Statment
CASE is used to match multiple conditions in a single construct.

SELECT
Name
, Case when POS = 1 then First
when POS = 2 then Second
when POS = 3 then Third
Else other end as Rank
FROM
ICC_batsmen_ranking
WHERE
POS > 5

DECODE is used to match conditions but allows only equal operators and is less flexible than CASE which allows other
operators too (LIKE, BETWEEN etc.)

SELECT
Name
,Decode (POS, 1, First, 2, Second, 3, Third, other) as Rank
FROM
ICC_batsmen_ranking
WHERE
POS > 5
Others
COMMENT/UNCOMMENT, USE_HASH
COMMENT/ UNCOMMENT

Single Line Comments: These comments start with -- and end in the same line
Multi Line Comments: These comments start with /* can span in multiple line and need to
be closed with */

USE_HASH

Used with Multi Line Comments Syntax /*+ USE_HASH ( alias1, alias2 ) */
This syntax enhances the performance of the query by pre-informing the compiler about the
tables and the related columns
The columns are sorted before performing operations like JOIN or GROUP BY
THANK YOU!

You might also like