Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Reference Manual

2014

Sachin Quickly Learns (SQL)

Reference Manual
Sachin Quickly Learns (SQL)
Krishna Kamath

14

Reference Manual

2014

Sachin Quickly Learns (SQL)

SQL REFERENCE MANUAL


This course is a tribute to Legendary Cricketer Sachin Ramesh Tendunkar. An attempt is
made to teach SQL [Structured Query Language] to all those who are interested to learn
Databases using Sachin Tendulkars International Test Cricket Scores.
SQL plays important role in our life. Let it be a bank transaction, railway ticket booking or
Statistics about Cricketers SQL keeps working for us. Interestingly, it is the simplest
programming language. If you are familiar with Excel, then learning database and SQL will
not take much time.In this course Database & SQL is explained with MS Access Database.

What is Database?
A database is an organized collection of data.
Database finds application in Most of business applications like library systems, flight
reservation systems, and computerized parts inventory systems.
Many databases have application software that accesses the database on behalf of end-users,
without exposing the DBMS interface directly.
Excel can also be used as database. For example now scores that you have in this excel sheet
is nothing but database. But in excel Score column you can enter remark also, that means you
can violate column properties in excel easily and hence it cannot be used raggedly as a
database.
There are varieties of databases Hierarchical database model, Network model, Relational
model, Etc.
There are Hundreds of database management systems like My SQL, Oracle, DB2, MS SQL,
MS Access, SQ Lite Etc.
If we learn one database properly then learning another database is not much complex as all
of these databases are designed around specifications defined around Structured Query
Language.
SQL is a special-purpose programming language designed for managing data held in a
relational database management system (RDBMS).
To understand Table we will take one example of storing information about Test Matches.
In this excel sheet you can find 3 tables , details about Sachin Tendulkars test career , all
the match information and all the stadium information of sachin tendulkar .

Reference Manual

2014

Sachin Quickly Learns (SQL)

Table 1 : Stadium_info: STADIUM_ID which is unique number given to each stadium


where sachin played, stadium_name , STADIUM_PLACE and COUNTRY.
Table 2: Match_info: Match_ID which is match number , STADIUM_ID,
START_DATE, END_DATE, TOSS, BAT here if bat is yes then team winning toss has
elected to bat first, RESULT, WON_BY, AGAINST country name,
MAN_OF_THE_MATCH.
Table 3:Sachin_details: Match_ID , INNINGS, BATTING_POSITION, OUT_TYPE,
BOWLING, SECOND_PLAYER who assisted bowler to take wicket, RUNS,balls
faced, MINUTES spent on crease , fours and sixes.

So Each Table will have a Multiple Column of Data.


Field: This basic unit of the table is called Field
RECORD: contains multiple scalar values, similar to a table record
Column: A vertical entity in a table that contains all information associated with a
specific field in a table.
TABLE: tabular structure with multiple columns and rows

Considering all these factors SQL can be broadly classified into,

Sl

Type of Query

Details
Commands that define a database, including creating,
altering, and dropping tables and establishing
constraints

Data Definition Language (DDL)

Data Manipulation
(DML)

Data Control Language (DCL)

Commands that control a database, including


administering privileges and committing data

Data Query Language (DQL)

The command to retrieve certain records from one or


more tables

Language Commands that manipulate data in a database


.example, update, insert

Try these following quarries to understand better.

Reference Manual

2014

Sachin Quickly Learns (SQL)

SELECT *
FROM STADIUM_INFO;

SELECT *
FROM STADIUM_INFO
WHERE COUNTRY = INDIA;

SELECT STADIUM_NAME, COUNTRY


FROM STADIUM_INFO
WHERE COUNTRY = INDIA;

INSERT INTO STADIUM_INFO


VALUES (72, GANGOTHRI GLADES CRICKET
GROUND, MYSORE, INDIA);

UPDATE STADIUM_INFO
SET STADIUM_NAME = GANGOTHRI GLADES CRICKET
GROUND, KARNATAKA
WHERE STADIUM_ID = 72;

DELETE *
FROM STADIUM_INFO
WHERE STADIUM_ID = 72;

Reference Manual

2014

Sachin Quickly Learns (SQL)

An operator is a reserved word or a character used to perform operation(s), such as Logical,


comparisons and arithmetic operations.
1.
2.
3.

Arithmetic operators
Comparison operators
Logical operators.

Arithmetic operators
Addition:
SELECT MATCH_ID,FOURS*4 + SIXES *6 AS RUNS_SCORED_IN_4SAND6S
FROM SACHIN_DETAILS;

Subtraction:
SELECT MATCH_ID,(RUNS-(FOURS*4 + SIXES *6)) AS
RUNS_SCORED_EXCLUDING4SAND6S
FROM SACHIN_DETAILS;

Multiplication:
SELECT MATCH_ID,FOURS*4 AS RUNS_SCORED_IN_4S
FROM SACHIN_DETAILS;

Division:
SELECT SUM(RUNS)/COUNT(MATCH_ID)
FROM SACHIN_DETAILS ;

Reference Manual

2014

Comparison operators
Equal to:
SELECT MATCH_ID,RUNS
FROM SACHIN_DETAILS
WHERE RUNS=100;
Greater Than:
SELECT MATCH_ID,RUNS
FROM SACHIN_DETAILS
WHERE RUNS>201;
Greater Than or Equal to:
SELECT MATCH_ID,RUNS
FROM SACHIN_DETAILS
WHERE RUNS>=201;
Less Than:
SELECT MATCH_ID,RUNS
FROM SACHIN_DETAILS
WHERE RUNS<10;
Less Than or Equal to:
SELECT MATCH_ID,RUNS
FROM SACHIN_DETAILS
WHERE RUNS<=10;

Not Equal to:


SELECT SIXES, MATCH_ID
FROM SACHIN_DETAILS
WHERE SIXES <> 0;

Sachin Quickly Learns (SQL)

Reference Manual

2014

Sachin Quickly Learns (SQL)

Logical operators
OR:
SELECT STADIUM_NAME, COUNTRY
FROM STADIUM_INFO
WHERE COUNTRY = INDIA OR COUNTRY = PAKISTAN ;
AND:
SELECT MATCH_ID,RUNS
FROM SACHIN_DETAILS
WHERE RUNS >= 100 AND BATTING_POSITION=4;
NOT:
SELECT MATCH_ID,RUNS
FROM SACHIN_DETAILS
WHERE NOT BATTING_POSITION = 4 ;
LIKE:
SELECT STADIUM_NAME,COUNTRY
FROM STADIUM_INFO
WHERE STADIUM_NAME LIKE *PARK;
IN:
SELECT OUT_TYPE,MATCH_ID,
BOWLING, RUNS
FROM SACHIN_DETAILS
WHERE BOWLING IN ("MCGRATH") ;

BETWEEN...AND:
SELECT MATCH_ID,RUNS
FROM SACHIN_DETAILS
WHERE RUNS BETWEEN 90 AND 99;

Reference Manual

2014

Sachin Quickly Learns (SQL)

Aggregate Functions
AVG:
SELECT AVG(RUNS)
FROM SACHIN_DETAILS
WHERE RUNS>100;
COUNT:
SELECT
COUNT( STADIUM_NAME)
FROM STADIUM_INFO;
FIRST:
SELECT
FIRST(RUNS)
FROM SACHIN_DETAILS;
LAST:
SELECT
LAST(RUNS)
FROM SACHIN_DETAILS;
MAX:
SELECT MAX(RUNS)
FROM SACHIN_DETAILS;
MIN:
SELECT MIN(RUNS)
FROM SACHIN_DETAILS
WHERE MATCH_ID BETWEEN 189 AND 200;
SUM:
SELECT SUM(RUNS) FROM SACHIN_DETAILS
WHERE MATCH_ID BETWEEN 150 AND 200;

Reference Manual

2014

Sachin Quickly Learns (SQL)

String Handling Functions


UCASE():
SELECT UCASE(STADIUM_NAME)
FROM STADIUM_INFO;
LCASE():
SELECT LCASE(BOWLING)
FROM SACHIN_DETAILS;
MID():
SELECT MID(COUNTRY,1,4)
FROM STADIUM_INFO;
LEN():
SELECT LEN(BOWLING),BOWLING
FROM SACHIN_DETAILS;

TRIM:
SELECT TRIM(RESULT)
FROM MATCH_INFO;
CONCATENATION:
SELECT
STADIUM_NAME & STADIUM_PLACE & COUNTRY
FROM STADIUM_INFO;

ALIAS:
SELECT MATCH_ID AS MATCH,
RUNS AS RUNS_SCORED,BALLS AS BALLS_FACED,
FOURS AS 4S,SIXES AS 6S
FROM SACHIN_DETAILS;

Reference Manual

2014

Sachin Quickly Learns (SQL)

Expressions
Percentage of scores from Boundaries:
SELECT RUNS, FOURS*4 + SIXES *6
AS BOUNDARY_SCORE,
FORMAT ((FOURS * 4)+ (SIXES * 6) / (RUNS * 100),"0.00")
AS PERCENTAGE_SCORE
FROM SACHIN_DETAILS
WHERE NOT RUNS=0;

WILDCARD
% OR * WILDCARD:

SELECT STADIUM_NAME
FROM STADIUM_INFO
WHERE COUNTRY LIKE 'A*A';

[CHARLIST] :

SELECT BOWLING
FROM SACHIN_DETAILS
WHERE BOWLING LIKE '[A-D]*';

[!CHARLIST] :
SELECT BOWLING
FROM SACHIN_DETAILS
WHERE BOWLING LIKE '[!A-S]*';

10

Reference Manual

2014

Sachin Quickly Learns (SQL)

Group by
SELECT MATCH_INFO.AGAINST,
FORMAT( AVG(SACHIN_DETAILS.RUNS),"00.00")AS RUNS_PERCENTAGE
FROM SACHIN_DETAILS LEFT JOIN MATCH_INFO
ON SACHIN_DETAILS.MATCH_ID= MATCH_INFO.MATCH_ID
GROUP BY AGAINST;

Having
SELECT MATCH_INFO.AGAINST,
COUNT(SACHIN_DETAILS.RUNS) AS CENTURIES
FROM SACHIN_DETAILS INNER JOIN MATCH_INFO
ON SACHIN_DETAILS.MATCH_ID=MATCH_INFO.MATCH_ID
WHERE SACHIN_DETAILS.RUNS>=100 GROUP BY AGAINST
HAVING NOT COUNT(SACHIN_DETAILS.RUNS)=0;

Order by
Ascending order:
SELECT RUNS
FROM SACHIN_DETAILS
WHERE RUNS>=200
ORDER BY RUNS;
Descending order:
SELECT RUNS
FROM SACHIN_DETAILS
WHERE RUNS>=200
ORDER BY RUNS DESC;

11

Reference Manual

2014

Sachin Quickly Learns (SQL)

Join :
SELECT SACHIN_DETAILS.RUNS, MATCH_INFO.AGAINST
FROM MATCH_INFO
INNER JOIN SACHIN_DETAILS
ON MATCH_INFO.MATCH_ID = SACHIN_DETAILS.MATCH_ID
WHERE SACHIN_DETAILS.RUNS >=100;

SELECT STADIUM_INFO.*, MATCH_INFO.AGAINST


FROM STADIUM_INFO
LEFT JOIN MATCH_INFO
ON STADIUM_INFO.STADIUM_ID = MATCH_INFO.STADIUM_ID;

Union Join :
SELECT *
FROM SACHIN_DETAILS , MATCH_INFO
WHERE MATCH_INFO.MATCH_ID=SACHIN_DETAILS.MATCH_ID
AND RUNS =0 AND OUT_TYPE<>'DNB'
UNION
SELECT *
FROM SACHIN_DETAILS , MATCH_INFO
WHERE MATCH_INFO.MATCH_ID=SACHIN_DETAILS.MATCH_ID
AND RUNS >=100;

12

Reference Manual

2014

Sachin Quickly Learns (SQL)

Sub Queries
SELECT SACHIN_DETAILS.*, M1.AGAINST
FROM MATCH_INFO AS M1, SACHIN_DETAILS
WHERE M1.MATCH_ID = SACHIN_DETAILS.MATCH_ID
AND RUNS > ( SELECT AVG(RUNS)
FROM SACHIN_DETAILS WHERE OUT_TYPE <> 'DNB' );

SELECT SACHIN_DETAILS.*, M1.AGAINST


FROM MATCH_INFO AS M1, SACHIN_DETAILS
WHERE M1.MATCH_ID = SACHIN_DETAILS.MATCH_ID
AND RUNS > ( SELECT AVG(RUNS)
FROM MATCH_INFO, SACHIN_DETAILS
WHERE MATCH_INFO.MATCH_ID = SACHIN_DETAILS.MATCH_ID
AND M1.AGAINST =MATCH_INFO.AGAINST
AND SACHIN_DETAILS.OUT_TYPE <> 'DNB');

So, Life is Simple. Technology tools, save a lot of time for us. If you have understood
building Queries, now you are not far from developing Database applications. We in
ERachana Software have developed an application named Tremplin, using which you can
now build a complete database without single line of manual coding. To build applications
using Tremplin you just need to know to Create Databases and Build SQL. If you know
databases without learning .Net Programming Language you can develop complete
application and that too in very short time. If you find that interesting, you can take Udemy
Course about Tremplin.
In this example database we have copied data from Excel and Pasted it Let us say you want to
build a database application to capture Player records Match by match and instantly see his
detailed statistics yourself, then you can build a simple application and at the end of the
match if you enter the details, you will get that players updated details. This application was
developed using Tremplin in less than 2 hours. You can download the sample application by

13

Reference Manual

2014

Sachin Quickly Learns (SQL)

visiting www.erachana.net. We have given virat kholi & M S Dhonis data in Sample and it
has his one day international details till 31st December 2013. This application is free and you
can download a complete application with source code.
Benefits of a Standardized Relational Language

Reduced training costs


Productivity
Application portability
Application longevity
Reduced dependence on a single vendor
Cross-system communication

You might have comfortably sit back and Learnt SQL. May be you will forget it if you dont
do anything practically. Imagine one the database application and create a database and write
some SQL statement. If you send an email after writing some queries to me at
krishna@erachana.net. I will be happy that my efforts were helpful to you.

14

You might also like