0% found this document useful (0 votes)
8 views

Xii Ip

Uploaded by

sourabku143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Xii Ip

Uploaded by

sourabku143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

INTRODUCTION TO SQL

BY-HENA FATMA
PGT CS
Sql commands

SQL defines following data languages to manipulate data of RDBMS.

 DDL : Data Definition Language


 DML : Data Manipulation Language
 TCL : Transaction Control Language
 DCL : Data Control Language
 DQL : Data Query Language
SQL Statements

 DRL - Retrieves data from the database

 DML - Enters new rows, changes existing rows, and removes unwanted rows from tables in
the database, respectively.

 DDL - Sets up, changes, and removes data structures from tables.

 TCL - Manages the changes made by DML statements. Changes to the data can be grouped
together into logical transactions.

 DCL - Gives or removes access rights to both the Oracle database and the structures within
it.
DDL : Data Definition Language

 All DDL commands are auto-committed. That means it saves all the
changes permanently in the database

Command Description

Create to create new table or database

Alter for alteration

Truncate delete data from table

Drop to drop a table

Rename to rename a table


DML : Data Manipulation Language

 Data Manipulation Language (DML) statements are used for managing data in
database. DML commands are not auto-committed. It means changes made
by DML command are not permanent to database, it can be rolled back.

Command Description

Insert to insert a new row

Update to update existing row

Delete to delete a row

Merge merging two rows or two tables


Basic structure of an SQL query

General SELECT, ALL / DISTINCT, *, AS, FROM, WHERE


Structure

Comparison IN, BETWEEN, LIKE "% _"

Grouping GROUP BY, HAVING,


COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )

Display Order ORDER BY, ASC / DESC

Logical AND, OR, NOT


Operators

Output INTO TABLE / CURSOR


TO FILE [ADDITIVE], TO PRINTER, TO SCREEN

Union UNION
General Structure

SELECT ...... FROM ...... WHERE ......


SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;
FROM tablename WHERE condition
Input Tables

Selecting All Columns


10

Example 1:
City Year Cars_sold

SELECT city, year, cars_sold Dhahran 2001 525


FROM car_sales; Dhahran 2002 456

 Can use * as an abbreviation for 'all Riyadh 2001 700


columns': Riyadh 2002 654

Jeddah 2001 921


Example 2:
Jeddah 2002 752
SELECT *
FROM car_sales; Khobar 2002
Dr. Ejaz Ahmed
Column Alias

 Renames a column heading

 Specify the alias after the column using a space as a separator.


there can also be the optional AS keyword between the column
name and alias

 Requires double quotation marks if it contains spaces or


special characters or is case sensitive

 By default, alias headings appear in uppercase. If the alias


contains spaces or special characters (such as # or $), or is case
sensitive, enclose the alias in double quotation marks (" ").
Input Tables

13 Selecting All Rows

 A SELECT statement without a


WHERE clause selects all rows. City Year Cars_Sold

Dhahran 2001 525


Example: Dhahran 2002 456

Riyadh 2001 700


SELECT * Riyadh 2002 654
FROM car_sales;
Jeddah 2001 921

Jeddah 2002 752


Khobar 2002

Dr. Ejaz Ahmed


WHERE clause
Where clause is used to specify condition while retriving data from table. Where clause is used
mostly with Select, Update and Delete query. If condititon specified by where clause is true then only
the result from table is returned.
Syntax for WHERE clause
SELECT column-name1,
column-name2,
column-name3,
column-nameN
from table-name WHERE [condition];
18 SELECT Definition …
 A SELECT statement can consist up to six clauses.

SELECT [DISTINCT | ALL]


{* | [column_expression [AS new_name]] [,...] }
FROM table_name [alias] [, ...]
[WHERE condition]
[GROUP BY column_list]
[HAVING condition]
[ORDER By column_list]

 Only SELECT and FROM clauses are mandatory.

 Order of the clauses cannot be changed.


Dr. Ejaz Ahmed
PRACTICE QUESTIONS:
Example using WHERE clause
Consider a Student table,

s_id s_Name age address

101 Adam 15 Noida

102 Alex 18 Delhi

103 Abhi 17 Rohtak

104 Ankit 22 Panipat

Now we will use a SELECT statement to display data of the table, based on a condition, which we
will add to the SELECT query using WHERE clause.
SELECT s_id,
s_name,
age,
address
from Student WHERE s_id=101;

s_id s_Name age Address

101 Adam 15 Noida


Distinct keyword
The distinct keyword is used with Select statement to retrieve unique values from the
table. Distinctremoves all the duplicate records while retrieving from database.
Syntax for DISTINCT Keyword
SELECT distinct column-name from table-name;
Example
Consider the following Emp table.

Eid name age Salary

401 Anu 22 5000

402 Shane 29 8000

403 Rohan 34 10000

404 Scott 44 10000

405 Tiger 35 8000


SELECT * from Emp WHERE salary < 10000 AND age > 25
The above query will return records where salary is less than 10000 and age greater than 25.

Eid name age Salary

402 Shane 29 8000

405 Tiger 35 9000


THANK
YOU

You might also like