0% found this document useful (0 votes)
86 views49 pages

Presentation On SQL

This document provides an overview of SQL (Structured Query Language) in 3 sentences: SQL is a standard language for managing database systems and includes commands for querying, manipulating, and defining data in databases. Key SQL statements are SELECT to query data, INSERT and UPDATE to modify data, DELETE to remove data, and CREATE TABLE to define a new table structure. The document then provides examples of using SQL statements like SELECT, WHERE, ORDER BY, INSERT, UPDATE, and DELETE to work with data in database tables.

Uploaded by

Lokesh Ahuja
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views49 pages

Presentation On SQL

This document provides an overview of SQL (Structured Query Language) in 3 sentences: SQL is a standard language for managing database systems and includes commands for querying, manipulating, and defining data in databases. Key SQL statements are SELECT to query data, INSERT and UPDATE to modify data, DELETE to remove data, and CREATE TABLE to define a new table structure. The document then provides examples of using SQL statements like SELECT, WHERE, ORDER BY, INSERT, UPDATE, and DELETE to work with data in database tables.

Uploaded by

Lokesh Ahuja
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 49

PRESENTATION

ON
SQL

Hewitt Associates 1 [DocID]


DEFINATION:-

SQL is a Standard –But……………


SQL is an ANSI (American National Standards Institute) standard
computer language for accessing and manipulating database systems.
SQL statements are used to retrieve and update data in a database. SQL
works with database programs like MS Access, DB2, Informix, MS SQL
Server, Oracle, Sybase, etc.

Hewitt Associates 2 [DocID]


SQL Database Tables
A database most often contains one or more tables. Each table is identified by a name (e.g.
"Customers" or "Orders"). Tables contain records (rows) with data.
Below is an example of a table called "Persons":

Hewitt Associates 3 [DocID]


SQL Data Manipulation Language (DML)

SQL (Structured Query Language) is a syntax for executing queries. But


the SQL language also includes a syntax to update, insert, and delete
records.
These query and update commands together form the Data Manipulation
Language (DML) part of SQL:

SELECT - extracts data from a database table


UPDATE - updates data in a database table
DELETE - deletes data from a database table
INSERT INTO - inserts new data into a database table

Hewitt Associates 4 [DocID]


SQL Data Definition Language (DDL)

The most important DDL statements in SQL are: 

CREATE TABLE - creates a new database table


ALTER TABLE - alters (changes) a database table
DROP TABLE - deletes a database table
CREATE INDEX - creates an index (search key)
DROP INDEX  - deletes an index

Hewitt Associates 5 [DocID]


SQL STATEMENTS

The SELECT Statement

The SELECT statement is used to


select data from a table. The tabular
result is stored in a result table (called
the result-set).

Syntax

Hewitt Associates 6 [DocID]


Select Some Columns

To select the columns named


"LastName" and "FirstName", use a
SELECT statement like this:

SELECT LastName, FirstName


FROM Persons
"Persons" table

Hewitt Associates 7 [DocID]


Result

Hewitt Associates 8 [DocID]


Semicolon after SQL Statements?

Semicolon is the standard way to separate each SQL statement in database


systems that allow more than one SQL statement to be executed in the
same call to the server.

Some SQL tutorials end each SQL statement with a semicolon. Is this
necessary? We are using MS Access and SQL Server 2000 and we do not
have to put a semicolon after each SQL statement, but some database
programs force you to use it.

Hewitt Associates 9 [DocID]


The SELECT DISTINCT Statement

The DISTINCT keyword is used to


return only distinct (different) values.
The SELECT statement returns
information from table columns. But
what if we only want to select distinct
elements?
With SQL, all we need to do is to add a
DISTINCT keyword to the SELECT
statement:

Hewitt Associates 10 [DocID]


Using the DISTINCT keyword

To select ALL values from the column named "Company" we use a SELECT
statement like this:
SELECT Company FROM Orders

"Orders" table

Result (Note that "W3Schools" is listed twice in the result-set.)

Hewitt Associates 11 [DocID]


To select only DIFFERENT values from the column named
"Company" we use a SELECT DISTINCT statement like this:
SELECT DISTINCT Company FROM Orders

Result: ( Now "W3Schools" is listed only once in the


result-set.)

The WHERE clause is used to specify a selection criterion.

Hewitt Associates 12 [DocID]


The WHERE Clause 

To conditionally select data from a


table, a WHERE clause can be
added to the SELECT statement.

Syntax

SELECT column FROM table


WHERE column operator value

With the WHERE clause, the


following operators can be used:

Hewitt Associates 13 [DocID]


Using the WHERE Clause

To select only the persons living in the city "Sandnes", we add a WHERE clause to the
SELECT statement: 

SELECT * FROM PersonsWHERE City='Sandnes‘

Result

Hewitt Associates 14 [DocID]


The LIKE Condition
The LIKE condition is used to specify a search for a pattern in a column.
Syntax
SELECT column FROM tableWHERE column LIKE patternA
"%" sign can be used to define wildcards (missing letters in the
pattern) both before and after the pattern.

Using LIKE

The following SQL statement will return persons with first names that
start with an 'O':

SELECT * FROM PersonsWHERE FirstName LIKE 'O%‘

Hewitt Associates 15 [DocID]


The following SQL statement will return persons with first names that end
with an 'a':

SELECT * FROM PersonsWHERE FirstName LIKE '%a

'The following SQL statement will return persons with first names that
contain the pattern 'la':

SELECT * FROM PersonsWHERE FirstName LIKE '%la%'

Hewitt Associates 16 [DocID]


The INSERT INTO Statement

The INSERT INTO statement is used to insert new rows into a table.
Syntax
INSERT INTO table_nameVALUES (value1, value2,....)

You can also specify the columns for which you want to insert data:
INSERT INTO table_name (column1, column2,...)VALUES (value1, value2,....)The
Update Statement
The UPDATE statement is used to modify the data in a table.
Syntax
UPDATE table_nameSET column_name = new_valueWHERE column_name =
some_valueUpdate several Columns in a Row

We want to change the address and add the name of the city:
UPDATE PersonSET Address = 'Stien 12', City = 'Stavanger'WHERE LastName =
'Rasmussen'

Hewitt Associates 17 [DocID]


Result:

Hewitt Associates 18 [DocID]


The Delete Statement
The DELETE statement is used to delete rows in a table.
Syntax

DELETE FROM table_nameWHERE column_name = some_valueDelete a Row


"Nina Rasmussen" is going to be deleted:

DELETE FROM Person WHERE LastName = 'Rasmussen'Result

Delete All Rows


It is possible to delete all rows in a table without deleting the table. This means that the table
structure, attributes, and indexes will be intact:

DELETE FROM table_nameorDELETE * FROM table_nameSort the Rows


The ORDER BY clause is used to sort the rows.

Hewitt Associates 19 [DocID]


Orders:

Example

To display the companies in alphabetical order:

SELECT Company, OrderNumber FROM


OrdersORDER BY Company
Example
To display the companies in alphabetical order
AND the ordernumbers in numerical order:

SELECT Company, OrderNumber FROM


OrdersORDER
BY Company, OrderNumber
Result:

Hewitt Associates 20 [DocID]


Example
To display the companies in reverse alphabetical order:
SELECT Company, OrderNumber FROM OrdersORDER BY Company
DESCResult:

AND & OR
AND and OR join two or more conditions in a WHERE clause.
The AND operator displays a row if ALL conditions listed are true. The OR
operator displays a row if ANY of the conditions listed are true.

Original Table (used in the examples)

Hewitt Associates 21 [DocID]


Example

Use AND to display each person with the first name equal to "Tove", and the last
name equal to "Svendson":
SELECT * FROM PersonsWHERE FirstName='Tove'AND
LastName='Svendson‘
Result:

Hewitt Associates 22 [DocID]


BETWEEN ... AND

The BETWEEN ... AND operator selects a range of data between two values. These values can be numbers,
text, or dates.

SELECT column_name FROM table_nameWHERE column_nameBETWEEN value1 AND value2


Example 1
To display the persons alphabetically between (and including) "Hansen" and exclusive "Pettersen", use the
following SQL:
SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen‘
Result:

Hewitt Associates 23 [DocID]


With SQL, aliases can be used for column names and
table names.

Column Name Alias


The syntax is:
SELECT column AS column_alias FROM table
Table Name Alias
The syntax is:
SELECT column FROM table AS table_alias
Example: Using a Column Alias
And this SQL:
SELECT LastName AS Family, FirstName AS NameFROM PersonsReturns this
Result:

Hewitt Associates 24 [DocID]


Joins and Keys

Sometimes we have to select data from two or more tables to make our result complete. We
have to perform a join.
Tables in a database can be related to each other with keys. A primary key is a column with a
unique value for each row. The purpose is to bind data together, across tables, without
repeating all of the data in every table.
In the "Employees" table below, the "Employee_ID" column is the primary key, meaning
that no two rows can have the same Employee_ID. The Employee_ID distinguishes two
persons even if they have the same name.
When you look at the example tables below, notice that: 
The "Employee_ID" column is the primary key of the "Employees" table
The "Prod_ID" column is the primary key of the "Orders" table
The "Employee_ID" column in the "Orders" table is used to refer to the persons in the
Employees" table without using their names

Hewitt Associates 25 [DocID]


EXAMPLE

Employees:

Orders:

Hewitt Associates 26 [DocID]


Referring to Two Tables
We can select data from two tables by referring to two tables, like this:
Example
Who has ordered a product, and what did they order?

SELECT Employees.Name, Orders.ProductFROM Employees, OrdersWHERE


Employees.Employee_ID=Orders.Employee_ID

Result

Hewitt Associates 27 [DocID]


Example
Who ordered a printer?

SELECT Employees.NameFROM Employees, OrdersWHERE


Employees.Employee_ID=Orders.Employee_IDAND
Orders.Product='Printer‘
Result

Hewitt Associates 28 [DocID]


Using Joins

OR we can select data from two tables with the JOIN keyword, like this:
Example INNER JOIN
Syntax
SELECT field1, field2, field3FROM first_tableINNER JOIN second_tableON first_table.keyfield =
second_table.foreign_keyfield
Who has ordered a product, and what did they order?
SELECT Employees.Name, Orders.ProductFROM EmployeesINNER JOIN OrdersON
Employees.Employee_ID=Orders.Employee_ID

The INNER JOIN returns all rows from both tables where there is a match. If there are rows in Employees
that do not have matches in Orders, those rows will not be listed.

Hewitt Associates 29 [DocID]


LEFT JOIN

Syntax
SELECT field1, field2, field3FROM first_tableLEFT JOIN second_tableON
first_table.keyfield = second_table.foreign_keyfield

EXAMPLE
List all employees, and their orders - if any.
SELECT Employees.Name, Orders.ProductFROM EmployeesLEFT JOIN OrdersON
Employees.Employee_ID=Orders.Employee_ID

Hewitt Associates 30 [DocID]


RIGHT JOIN

Syntax
SELECT field1, field2, field3FROM first_tableRIGHT JOIN second_tableON
first_table.keyfield = second_table.foreign_keyfield

List all orders, and who has ordered - if any.

SELECT Employees.Name, Orders.ProductFROM EmployeesRIGHT JOIN


OrdersON Employees.Employee_ID=Orders.Employee_ID

Hewitt Associates 31 [DocID]


Joins and Keys

Sometimes we have to select data from two or more tables to make our result complete. We
have to perform a join.
Tables in a database can be related to each other with keys. A primary key is a column with a
unique value for each row. The purpose is to bind data together, across tables, without
repeating all of the data in every table.
In the "Employees" table below, the "Employee_ID" column is the primary key, meaning
that no two rows can have the same Employee_ID. The Employee_ID distinguishes two
persons even if they have the same name.
When you look at the example tables below, notice that: 
The "Employee_ID" column is the primary key of the "Employees" table
The "Prod_ID" column is the primary key of the "Orders" table
The "Employee_ID" column in the "Orders" table is used to refer to the persons in the
"Employees" table without using their names

Hewitt Associates 32 [DocID]


Referring to Two Tables

Employees:

Orders:

We can select data from two tables by referring to two tables, like this:

Hewitt Associates 33 [DocID]


Example

Who has ordered a product, and what did


they order?
SELECT Employees.Name,
Orders.ProductFROM Employees, Orders
WHERE
Employees.Employee_ID=Orders.E
mployee_IDResult
Who ordered a printer?
SELECT Employees.NameFROM
Employees, OrdersWHERE
Employees.Employee_ID=Orders.E
mployee_IDAND
Orders.Product='Printer'

Hewitt Associates 34 [DocID]


INNER JOIN

Syntax
SELECT field1, field2, field3FROM
first_tableINNER JOIN second_
tableON first_table.keyfield =
second_table.foreign_keyfield
Who has ordered a product, and what did they
order?
SELECT Employees.Name,
Orders.ProductFROM EmployeesINNER
JOIN OrdersON
Employees.Employee_ID=Orders.Employee_I
D
The INNER JOIN returns all rows from both
tables where there is a match. If there are rows in
Employees that do not have matches in Orders,
those rows will not be listed.
Result

Hewitt Associates 35 [DocID]


LEFT JOIN

Syntax
SELECT field1, field2, field3FROM
first_tableLEFT JOIN second_tableON
first_table.keyfield =
second_table.foreign_keyfield
List all employees, and their orders - if any.

SELECT Employees.Name,
Orders.ProductFROM EmployeesLEFT JOIN
OrdersON
Employees.Employee_ID=Orders.Employee_I
D

The LEFT JOIN returns all the rows from the first
table (Employees), even if there are no matches in
the second table (Orders). If there are rows in
Employees that do not have matches in Orders,
those rows also will be listed.

Hewitt Associates 36 [DocID]


RIGHT JOIN

Syntax
SELECT field1, field2, field3FROM
first_tableRIGHT JOIN second_tableON
first_table.keyfield =
second_table.foreign_keyfield

List all orders, and who has ordered - if any.

SELECT Employees.Name,
Orders.ProductFROM EmployeesRIGHT JOIN
OrdersON
Employees.Employee_ID=Orders.Employee_ID

The RIGHT JOIN returns all the rows from the


second table (Orders), even if there are no matches
in the first table (Employees). If there had been any
rows in Orders that did not have matches in
Employees, those rows also would have been listed.

Hewitt Associates 37 [DocID]


Create a Database

To create a database:

CREATE DATABASE database_name

Create a Table

To create a table in a database:

CREATE TABLE table_name(column_name1


data_type,column_name2 data_type,.......)

Hewitt Associates 38 [DocID]


Drop Index

You can delete an existing index in a table with the DROP statement.

DROP INDEX table_name.index_name

Delete a Table or Database


To delete a table (the table structure, attributes, and indexes will also be
deleted):

DROP TABLE table_nameTo delete a database:


DROP DATABASE database_name

Hewitt Associates 39 [DocID]


Truncate a Table

What if we only want to get rid of the data inside a table, and not the
table itself?

Use the TRUNCATE TABLE command (deletes only the data inside
the table):

TRUNCATE TABLE table_name

Hewitt Associates 40 [DocID]


ALTER TABLE

The ALTER TABLE statement is used to add or drop columns in an


existing table.

ALTER TABLE table_name ADD column_name datatype,


ALTER TABLE table_name DROP COLUMN column_name

Note:
Some database systems don't allow the dropping of a column in a
database table (DROP COLUMN column_name).

Hewitt Associates 41 [DocID]


GROUP BY...

GROUP BY... was added to SQL because


aggregate functions (like SUM) return the
aggregate of all column values every time they
are called, and without the GROUP BY
function it was impossible to find the sum for
each individual group of column values.

The syntax for the GROUP BY function is:

SELECT column,SUM(column) FROM table


GROUP BY column
GROUP BY
Example
This "Sales" Table:

Hewitt Associates 42 [DocID]


And This SQL:
SELECT Company, SUM(Amount) FROM Sales

Returns this result:

The above code is invalid because the column returned is


not part of an aggregate. A GROUP BY clause will solve
this problem:

Hewitt Associates 43 [DocID]


HAVING...

HAVING... was added to SQL because the WHERE keyword could not be used against
aggregate functions (like SUM), and without HAVING... it would be impossible to test for
result conditions.
The syntax for the HAVING function is:

This SQL:
SELECT Company,SUM(Amount) FROM SalesGROUP BY CompanyHAVING
SUM(Amount)>10000Returns this result:

Hewitt Associates 44 [DocID]


Make a Backup Copy

The following example makes a backup copy of the "Persons" table:

SELECT * INTO Persons_backupFROM Persons

The IN clause can be used to copy tables into another database:

SELECT Persons.* INTO Persons IN 'Backup.mdb'FROM Persons

Hewitt Associates 45 [DocID]


What is a View?

In SQL, a VIEW is a virtual table based on the result-set of a SELECT statement .

A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database. You can add SQL functions,
WHERE, and JOIN statements to a view and present the data as if the data were
coming from a single table.
Note: The database design and structure will NOT be affected by the functions,
where, or join statements in a view.
Syntax
CREATE VIEW view_name ASSELECT column_name(s)FROM
table_nameWHERE condition
Note: The database does not store the view data! The database engine recreates the
data, using the view's SELECT statement, every time a user queries a view.

Hewitt Associates 46 [DocID]


Using Views

A view could be used from inside a query, a stored procedure, or from inside
another view. By adding functions, joins, etc., to a view, it allows you to present
exactly the data you want to the user.
The sample database Northwind has some views installed by default. The view
"Current Product List" lists all active products (products that are not discontinued)
from the Products table. The view is created with the following SQL:
CREATE VIEW [Current Product List] ASSELECT
ProductID,ProductNameFROM ProductsWHERE Discontinued=No
We can query the view above as follows:
SELECT * FROM [Current Product

Hewitt Associates 47 [DocID]


Another view from the Northwind
sample database selects every product
in the Products table that has a unit
price that is higher than the average
unit price:

CREATE VIEW [Products Above


Average Price] ASSELECT
ProductName,UnitPriceFROM
ProductsWHERE UnitPrice>(SELECT
AVG(UnitPrice) FROM Products)We
can query the view above as follows:

SELECT * FROM [Products Above


Average Price]

Hewitt Associates 48 [DocID]


Hewitt Associates 49 [DocID]

You might also like