0% found this document useful (0 votes)
61 views125 pages

SQL With Examples

This document provides an overview of SQL (Structured Query Language). It discusses basic SQL queries using SELECT, FROM, and WHERE clauses. It also covers SQL statements like INSERT, UPDATE, DELETE. Additional topics include JOINs, aggregate functions, creating views, backups and restores. The goal of SQL is to retrieve and manipulate data from a relational database using simple, standardized SQL statements.

Uploaded by

Aanya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
61 views125 pages

SQL With Examples

This document provides an overview of SQL (Structured Query Language). It discusses basic SQL queries using SELECT, FROM, and WHERE clauses. It also covers SQL statements like INSERT, UPDATE, DELETE. Additional topics include JOINs, aggregate functions, creating views, backups and restores. The goal of SQL is to retrieve and manipulate data from a relational database using simple, standardized SQL statements.

Uploaded by

Aanya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 125

SQL

Structured Query Language


4

Basic Queries in SQL

• SQL has only one basic statement to retrieve


information
▫ SELECT
▫ FROM
▫ WHERE
5

The Select-From-Where Structure of


SQL Queries
• General Form:
▫ SELECT <attributes list>
▫ FROM <tables list>
▫ WHERE <condition>;
SELECT Operator
DISTINCT Statement
WHERE Clause
AND, OR Operator
x y x AND y x OR y Not x

TRUE TRUE TRUE TRUE FALSE

TRUE UNKNOWN UNKNOWN TRUE FALSE

TRUE FALSE FALSE TRUE FALSE

UNKNOWN TRUE UNKNOWN TRUE UNKNOWN

UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN

UNKNOWN FALSE FALSE UNKNOWN UNKNOWN

FALSE TRUE FALSE TRUE TRUE

FALSE UNKNOWN FALSE UNKNOWN TRUE

FALSE FALSE FALSE FALSE TRUE


ORDER BY Keyword
INSERT INTO Statement
UPDATE Statement
DELETE Statement
TOP Clause
MYSQL Syntax
SELECT TOP ROWNUM COLUMN_NAME(S)
FROM TABLE NAME
LIKE Operator
WILDCARDS
Ba
And total number of characters are 6.

Where last name like ‘____ri’


IN Operator
BETWEEN Operator
ALIAS
AND

AND
JOIN Operator
LEFT JOIN Keyword
UNION Operator
Aggregate Functions
First()
• SELECT column_name FROM table_name
ORDER BY column_name ASC
WHERE ROWNUM <=1;

Last()
• SELECT column_name FROM table_name
ORDER BY column_name DESC
WHERE ROWNUM <=1;
kumari
kumar
gubbi
Oracle Syntax
SELECT SUBSTR(City,1,4) From Persons;
Oracle Syntax

SELECT Length(Address) From Persons;


CREATE BACKUP and RESTORE
BACKUP: mysqldump --opt -u [uname]
-p[pass] [dbname] > [backupfile.sql]

Exmple: mysqldump –u root –p lab1>lab1_backup.sql

RESTORE: mysqldump --opt -u [uname]


-p[pass] [dbname] < [backupfile.sql]

Exmple: mysqldump –u root –p lab1<lab1_backup.sql


CREATE TABLE Statement
CONSTRAINTS
CREATE VIEW
Create View 124

• CREATE VIEW View Name AS (SELECT • SELECT * FROM View_Person WHERE Id


column_name(s) FROM Table_Name); BETWEEN 4 AND 8;
Oracle will transform the query as follows :
• SELECT * FROM (SELECT * FROM PersonInfo
Where City Not In ('LUCKNOW', 'DELHI'))
WHERE Id BETWEEN 4 AND 8;

• CREATE VIEW View _Person AS SELECT * FROM


PersonInfo Where City Not In ('LUCKNOW', 'DELHI');
• SELECT * FROM View_Person;

You might also like