You are on page 1of 43

Switch off your Mobiles

Phones
or
Change Profile to Silent
Mode
Writing Basic Sql Statement
 SQL Fundamentals
 SQL Characterization
 SQL SELECT Statement
Sql Fundamentals
 Structured Query Language (SQL) is a standard language for
accessing and manipulating databases.

 SQL has been standardised by ANSI and has been adopted as an


industry standard for relational database systems.

 The basic structure of data stored in any database is in the form of


.
Sql Fundamentals
 A particular table in any database consists of and

 A row in a table represents set of data associated with a


particular entity.
 A column in a table represents the characteristics of the data.
SQL Characterization
 SQL comprised of various statements to manage and create
databases.

 The SQL statements are characterised under two main heads


these are:
Data Manipulation Language (DML)
Data Definition Language (DDL)
Besides these DCL(Data Control Language),
TCL(Transaction Control Language),
DQL(Data Query Language) are also used.
SQL Characterization
• Data Manipulation Language (DML) is used to retrieve, modify and
insert information from or into the database.

• The SQL statements which fall under the DML category are as
follows:
SELECT Statement
INSERT Statement
UPDATE Statement
DELETE Statement
SQL Select Statement
 SQL Select statement is used to retrieve information from the
database table.

 Syntax:
SELECT [* | DISTINCT] columnname1 [, columnname2]
FROM tablename1 [, tablename2]
[WHERE condition] [And | or | LIKE condition . . .]
[GROUP BY column-list]
[HAVING “conditions”]
[ORDER BY “condition-list” [ASC | DESC]]
SQL Select Statement
SQL Select Statement
 A simpler syntax for Select statement can be:

SELECT columnname1 [, columnname2] FROM tablename1 [,


tablename2]

Example:
S E L E C T e m p lo y e e _ id , fir s t _ n a m e , la s t _ n a m e , j o b _ i d f r o m
employees;
SQL Select Statement
Capabilities of SQL Select Statements

Projection

Selection

Joining
SQL Select Statement
 Displaying All columns in a table.

SELECT * FROM JOBS;


OR
SELECT JOB_ID, JOB_TITLE, MIN_SALARY, MAX_SALARY FROM
JOBS;
Displaying specific
column
• Displaying specific columns in a table
Assigning alias to a
column
Displaying data with
arithmetic calculation
Concatenating Two
columns
Displaying Data using
Literals
Displaying Distinct
Records
Using Dual Table
 SELECT SYSDATE, USER FROM dual;

• SELECT SYSDATE, USER FROM dual;


RESTRICTING AND
SORTING DATA
Limiting Rows
• The mechanism of restricting data being displayed is known as
limiting rows.
• Rows to be displayed are restricted over certain criteria and only
those rows are displayed which satisfy the condition
• To perform restriction on the rows, a WHERE clause is used with
SELECT statement.
• Following the WHERE keyword is the condition which restrict the
rows.
• WHERE clause compares values in columns, literal values, arithmetic
expressions or functions.
• Cannot use alias name.
Limiting Rows
• Syntax:
SELECT [* | DISTINCT] columnname1 [, columnname2] FROM
tablename1 [, tablename2]
[WHERE condition] [AND | OR | LIKE condition . . .]

Example:
Limiting Rows
Finding top 5 highest paid employees
Using WHERE Clause
• WHERE clause uses different comparison operators:

• Simple Comparison Operator

• Logical Comparison Operator

• Other Comparison Operator


Simple comparison
Operators
Operator Description
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
ANY / SOME Compares two a value to each value in
List or subquery.
ALL Compares a value to every value in a
list or subquery.
Simple comparison
Operators
Using Where Clause
• Logical AND comparison operator: It is used to compare two or more
conditions for limiting rows.
• Both the conditions are required to be to get a result.
• Logical OR Operator: same as AND, but returns ALL records
matching either of the conditions.
• Logical NOT operator: It returns the result set by negating
the condition.
Examples
• SELECT * FROM EMPLOYEES WHERE JOB_ID=‘SH_CLERK’ and
DEPARTMENT_ID=50;

• SELECT * FROM EMPLOYEES WHERE JOB_ID=‘SH_CLERK’ OR


JOB_ID=‘ST_CLERK’ ;

• SELECT * FROM EMPLOYEES WHERE NOT( JOB_ID=‘SH_CLERK’


OR JOB_ID=‘ST_CLERK’) ;
WHERE CLAUSE WITH
BETWEEN OPERATOR
EXAMPLE:
SELECT B_Code, Title, Price FROM Books
WHERE Price BETWEEN 11 AND 14;
WHERE CLAUSE WITH IN
OPERATOR
EXAMPLE:
SELECT B_Code, Title, Price FROM Books
WHERE Price IN (11, 12, 14);
WHERE CLAUSE WITH
EXISTS
EXAMPLE:
SELECT B_Code, Title FROM Books B
WHERE EXISTS (SELECT Quantity FROM Inventory I
WHERE I.B_Code = B.B_Code AND Quantity = 1);
WHERE CLAUSE WITH
ISNULL
• EXAMPLE:
SELECT * FROM Books
WHERE Paperback IS NULL;
• LIKE clause is used for searching character patterns

• This character matching is known as WILDCARD search


EXAMPLES
SELECT title
FROM Books
WHERE title LIKE ‘T_e%’ ;

SELECT FIRST_NAME FROM EMPLOYEES WHERE


FIRST_NAME LIKE ‘A%’ ;
PRECEDENCE RULE
SORTING ROWS
• Arranging data in an ascending or descending order is called
sorting.

• Data in a result set can be arranged in an order by using the


ORDER BY clause with the SELECT statement.

• By default the data is in ascending order.


Syntax:
SELECT [* | DISTINCT] columnname1 [,columnname2]
FROM tablename1 [, tablename2]
[ORDER BY “condition-list” [ASC | DESC]]
EXAMPLES
SELECT Fname, Lname FROM Author ORDER BY Fname ;
EXAMPLES
SELECT Fname, Lname FROM Author ORDER BY Fname DESC ;
ORDER BY WITH MULTIPLE
COLUMNS (EXAMPLES)
SELECT B_Code, title, P_Code, Price FROM Books
ORDER BY Price, title;
Any Questions ??

You might also like