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

SL SLLLC1 ULk

Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
Contents
1 Introduction
2 SELECT
3 SELECT (DISTINCT)
4 SELECT Logical Operation
4.1 Logical Operation:
4.2 Logical AND
4.3 Logical OR
4.4 Logical IN
4.5 Logical Between
5 SELECT LIKE PATTERN
6 SELECT ORDER BY
6.1 ORDER BY DESC
6.2 ORDER BY
7 SELECT ALIAS
7.1 Table Level Alias
7.2 Column Level Alias
8 Aggregate Functions
8.1 AVERAGE (AVG)
8.2 COUNT
8.3 MAX
8.4 MIN
8.5 SUM
8.6 GROUP BY
8.7 HAVING
1.Introduction to SQL
The statement is used to select data from a table. It is the most
used query in SQL and it can retrieve complete table data or partial data by
using clause in the Statement.
2 SELECT
Syntax: Specific Columns
SELECT col1,col2,col3,coln FROM <table_name>;
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
Example Specific Column:
CUSTOMER
FIRST_NAME LAST_NAME ADDRESS CITY COUNTRY BIRTH_DATE
John Adam
13 BAKE
STREET
London
United
Kingdom
12-AUG-87
Larry Hall
34 VILLE
PARLE
Mumbai India 12-JUN-90
Paul Amsterdam
SELECT city,birth_date FROM customer;
Query Result:
CUSTOMER
CITY BIRTH_DATE
London 12-AUG-87
Mumbai 12-JUN-90
Amsterdam
Syntax: All Columns
SELECT * FROM <table_name>;
Example All Columns:
SELECT * FROM CUSTOMER;
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
Query Result:
CUSTOMER
FIRST_NAME LAST_NAME ADDRESS CITY COUNTRY BIRTH_DATE
John Adam
13 BAKE
STREET
London
United
Kingdom
12-AUG-87
Larry Hall
34 VILLE
PARLE
Mumbai India 12-JUN-90
Paul Amsterdam
3 SELECT (DISTINCT)
Yields unique columns from a given table.
Example DISTINCT:
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT DISTINCT store_name
FROM STORE_INFORMATION;
Query Result:
STORE_INFORMATION
STORE_NAME
Los Angeles
Saint Diego
Boston
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
4 SELECT Logical Operation
Fetching Records according to the specified criteria.
4.1 Logical Operation:
SELECT * FROM STORE_INFORMATION
WHERE SALES>1000;
Query Result:
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
4.2 Logical AND
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
SELECT * FROM STORE_INFORMATION
WHERE DT='8-JAN-99' AND SALES=300;
Query Result:
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 300 8-JAN-99
4.3 Logical OR
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT * FROM STORE_INFORMATION
WHERE DT='8-JAN-99' AND DT='5-JAN-99';
Query Result:
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
4.4 Logical IN
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT * FROM STORE_INFORMATION
WHERE STORE_NAME IN('Boston','Saint Diego');
Query Result:
STORE_INFORMATION
STORE_NAME SALES DT
Saint Diego 250 7-JAN-99
Boston 700 8-JAN-99
4.5 Logical Between
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT * FROM STORE_INFORMATION
WHERE DT BETWEEN '01-JAN-99' AND '07-JAN-99';
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
Query Result:
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
5 SELECT LIKE PATTERN
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT * FROM STORE_INFORMATION
WHERE STORE_NAME LIKE 'Los%';
Query Result:
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Los Angeles 300 8-JAN-99
6 SELECT ORDER BY
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
6.1 ORDER BY DESC
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT * FROM STORE_INFORMATION
ORDER BY dt DESC;
Query Result:
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 1500 5-JAN-99
6.2 ORDER BY
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT * FROM STORE_INFORMATION ORDER BY SALES;
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
Query Result:
STORE_INFORMATION
STORE_NAME SALES DT
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
Los Angeles 1500 5-JAN-99
7 SELECT ALIAS
Alias is an alternative name given to column or table in SQL statements.
7.1 Table Level Alias
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT * FROM STORE_INFORMATION SI
WHERE SI.SALES>300;
Query Result:
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Boston 700 8-JAN-99
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
7.2 Column Level Alias
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT SUM(SALES) "TOTAL SALES"
FROM STORE_INFORMATION;
Query Result:
STORE_INFORMATION
TOTAL SALES
2750
8 Aggregate Functions
Alias is an alternative name given to column or table in SQL statements.
8.1 AVERAGE (AVG)
Average of the column value.
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
SELECT AVG(SALES) "AVG SALES"
FROM STORE_INFORMATION;
Query Result:
8.2 COUNT
This allows us to COUNT up the number of rows in a certain table.
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT COUNT(SALES) "ROW CNT"
FROM STORE_INFORMATION;
Query Result:
STORE_INFORMATION
ROW CNT
4
STORE_INFORMATION
AVG SALES
687.5
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
8.3 MAX
Maximum numerical value of the column.
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT MAX(SALES) "MAXIMUM SALES"
FROM STORE_INFORMATION;
Query Result:
STORE_INFORMATION
MAXIMUM SALES
1500
8.4 MIN
Minimum numerical value of the column.
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT MIN(SALES) "MINIMUM SALES"
FROM STORE_INFORMATION;
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
Query Result:
STORE_INFORMATION
MAXIMUM SALES
250
8.5 SUM
Returns the sum of values of a column.
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT SUM(SALES) "TOTAL SALES"
FROM STORE_INFORMATION;
Query Result:
STORE_INFORMATION
TOTAL SALES
2750
8.6 GROUP BY
The GROUP BY keyword is used when we are selecting multiple columns from a
table (or tables) and at least one arithmetic operator appears in the SELECT statement.
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT STORE_NAME,SUM(SALES) "TOTAL SALES"
FROM STORE_INFORMATION GROUP BY STORE_NAME;
Query Result:
STORE_INFORMATION
STORE_NAME TOTAL SALES
Los Angeles 1800
Saint Diego 250
Boston 700
8.7 HAVING
Similar To WHERE but it is reserved for aggregate function which can be used in
conjunction with GROUP BY clause.
STORE_INFORMATION
STORE_NAME SALES DT
Los Angeles 1500 5-JAN-99
Saint Diego 250 7-JAN-99
Los Angeles 300 8-JAN-99
Boston 700 8-JAN-99
SELECT STORE_NAME,SUM(SALES) "TOTAL SALES"
FROM STORE_INFORMATION GROUP BY STORE_NAME
HAVING SUM(SALES)>1500;
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
Query Result:
STORE_INFORMATION
STORE_NAME TOTAL SALES
Los Angeles 1800
Here the stores with total sales over 1,500 are fetched.
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
Get Free Computer Science Study Kit Android App From
OR
For more lecture notes and Computer Science Study Kit Android app
visit us at
http://computer-science-study-kit.blogspot.in/
We will be happy to have your comments and ideas about these notes and
app also and if you find these notes helpful, please do share them with your
friends and colleagues.
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
Get Free Computer Science Study Kit Android App From
OR
For more lecture notes and Computer Science Study Kit Android app
visit us at
http://computer-science-study-kit.blogspot.in/
We will be happy to have your comments and ideas about these notes and
app also and if you find these notes helpful, please do share them with your
friends and colleagues.
SL SLLLC1 ULk
Cet More Lecture notes and Computer Sclence Study klt Androld app lrom:
http://computer-sclence-study-klt.blogspot.ln/
Get Free Computer Science Study Kit Android App From
OR
For more lecture notes and Computer Science Study Kit Android app
visit us at
http://computer-science-study-kit.blogspot.in/
We will be happy to have your comments and ideas about these notes and
app also and if you find these notes helpful, please do share them with your
friends and colleagues.

You might also like