Вы находитесь на странице: 1из 57

Structured Query Language

SQL

SQL

SQL is used to communicate with a database SQL is a database computer language designed for the retrieval and management of data in RDBMS.

SQL was adopted as a standard by ANSI in 1986 and ISO in 1987.

Features of SQL

Non Procedural language:-Because no conditional


or iterative logic is involved.

Unified language:-Very simple ,English


like,commands for querying ,inserting , deleting and modifying data and objects.

Common languages for all relational databases.

SQL
SQL is made of three sublanguages.

Data Definition Language (DDL)


Data Manipulation Language (DML) Data Control Language(DCL)

Data Definition Language (DDL)

DDL is a computer language for defining data structures.

DDL is used to

Create a table Alter the structure of a table

Drop the table

DDL Commands

Create

Table

Alter
Drop

Table
Table

Rename

CREATE TABLE

To create a new table.

Must include table name and field details.

CREATE TABLE-Syntax
Create table <table name> (colname1 datatype(size), colname2 datatype(size), ..., colnamen datatype(size));

DataTypes
NUMBER CHAR VARCHAR

DATE

CREATE TABLE - Example


CREATE TABLE employees (eid number(3), first_name varchar(50), sex char(1), dateofbirth date);

ALTER TABLE

To modify an existing database object.


Add a column Drop a column Change a column size or data type for a column

ALTER TABLE- Syntax


Alter table <tablename> [Add <colname datatype(size)>] [Modify <colname datatype(size)>] [Drop column <columnname>]

ALTER TABLE-Example

ALTER TABLE employees ADD phone number(10); ALTER TABLE employees MODIFY eid number(5); ALTER TABLE employees DROP COLUMN phone;

DROP TABLE

A Drop statement in SQL removes an object from a relational database management system (RDBMS).

DROP TABLE - Syntax

DROP TABLE <table_name>

DROP TABLE -Example

DROP TABLE employee;

RENAME

To change the name of the table

RENAME - Syntax

RENAME <old_ table_name> To <new_table_name>

RENAME -Example

Rename employee to emp1;

Data Manipulation Language (DML)

DML is a family of computer languages used by computer programs database users to retrieve, insert, delete and update data in a database.

DML Commands

INSERT

UPDATE

DELETE

SELECT

INSERT-Syntax

INSERT INTO <table_name> [(column1, column2, ...,columnn)] VALUES (value1, value2, ...,valuen);

INSERT - Example

INSERT INTO phone_book (name, number) VALUES ('John Doe', 5551212);

UPDATE

You can modify the existing rows by using UPDATE statement.

Syntax UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value Example UPDATE emp SET dno=30 WHERE ename=smith;

DELETE

The DELETE statement is used to delete records in a table.

Syntax DELETE FROM <table_name> [WHERE <condition>]; Example DELETE FROM emp WHERE dno=10;

SELECT - Syntax

SELECT colname FROM <table_name> [WHERE <condition>]

OPERATORS

Relational Operators

Logical operators
Special Operators

Relational Operators

< > <= >= <> =

Logical operators

AND

OR
NOT

Special Operators

IN BETWEEN

LIKE

AGGREGATE FUNCTIONS

SQL aggregate functions return a single

value, calculated from values in a column.

AGGREGATE FUNCTIONS

AVG() - Returns the average value COUNT() - Returns the number of rows

FIRST() - Returns the first value


LAST() - Returns the last value MAX() - Returns the largest value

MIN() - Returns the smallest value


SUM() - Returns the sum

AVG() Function

The AVG() function returns the average value of a numeric column.

Syntax SELECT AVG(column_name) FROM table_name; Example: SELECT AVG(OrderPrice) as Average FROM Orders ;

COUNT()

The COUNT() function returns the number of rows that matches a specified criteria.

Syntax SELECT COUNT (column_name) FROM table_name; Example SELECT COUNT (order_no) FROM orders;

COUNT(*)

The COUNT(*) function returns the number of records in a table.

Syntax
SELECT COUNT(*) FROM table_name;

Example
SELECT COUNT(*) FROM Employee;

FIRST()

The FIRST() function returns the first value of the selected column.

Syntax SELECT FIRST (column_name) FROM table_name; Example SELECT FIRST (OrderPrice) FROM Orders ;

LAST()

The LAST() function returns the last value of the selected column.

Syntax SELECT LAST(column_name) FROM table_name; Example SELECT LAST(OrderPrice) FROM Orders ;

MAX()

The MAX() function returns the largest value of the selected column.

Syntax SELECT MAX(column_name) FROM table_name; Example SELECT MAX(OrderPrice) FROM Orders ;

MIN()

The MIN() function returns the smallest value of the selected column.

Syntax SELECT MIN (column_name) FROM table_name; Example SELECT MIN (OrderPrice) FROM Orders;

SUM()

The SUM() function returns the total sum of a numeric column.

Syntax SELECT SUM (column_name) FROM table_name; Example SELECT SUM (OrderPrice) FROM Orders;

SCALAR FUNCTIONS

SQL scalar functions return a single value, based on the input value. Useful scalar functions: 1. UCASE() 2. LCASE() 3. MID() 4. LEN() 5. ROUND() 6. NOW() 7. FORMAT()

Example Table
P_Id 1 2 3 LastName Hansen Svendson Pettersen Prod_Id 1 2 3 UCASE LCASE FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 Unit 1000 g 1000 g 1000 g UnitPrice 10.45 32.56 15.67 City Sandnes Sandnes Stavanger

ProductName Jarlsberg Mascarpone Gorgonzola

MID

NOW

LEN

ROUND

FORMAT

UCASE()
The UCASE() function converts the value of a field to uppercase. Syntax SELECT UCASE(column_name) FROM table_name; Example SELECT UCASE (LastName) as LastName, FirstName FROM Persons

LastName HANSEN SVENDSON

FirstName Ola Tove Example

PETTERSEN Kari

LCASE()
The LCASE() function converts the value of a field to lowercase. Syntax SELECT LCASE (column_name) FROM table_name; Example SELECT LCASE(LastName) as LastName, FirstName FROM Persons

LastName hansen svendson pettersen

FirstName Ola Tove Kari Example

MID()
The MID() function is used to extract characters from a text field. Syntax SELECT MID (column_name, start ,length]) FROM table_name;

Parameter column_name

Description Required. The field to extract characters from

start
length

Required. Specifies the starting position (starts at 1)


Optional. The number of characters to return. If omitted, the MID() function returns the rest of the text

Example

SELECT MID(City,1,4) as SmallCity FROM Persons

Example

SmallCity Sand

Sand
Stav

NOW()
The NOW() function returns the current system date and time. Syntax SELECT NOW() FROM table_name; Example SELECT ProductName, UnitPrice, Now() as PerDate FROM Products

ProductName Jarlsberg Mascarpone Gorgonzola

UnitPrice 10.45 32.56 15.67

PerDate 10/7/2008 11:25:02 AM 10/7/2008 11:25:02 AM 10/7/2008 11:25:02 AM Example

LEN()
The LEN() function returns the length of the value in a text field. Syntax SELECT LEN (column_name) FROM table_name; Example SELECT LEN (Address) as LengthOfAddress FROM Persons

LengthOfAddress 12 9 9 Example

ROUND()

The ROUND() function is used to round a numeric field to the number of decimals specified.

Syntax

SELECT ROUND(column_name,decimals)
FROM table_name;
Parameter column_name decimals Description Required. The field to round. Required. Specifies the number of decimals to be returned.

Example
SELECT ProductName, ROUND(UnitPrice,0) FROM Products
ProductName Jarlsberg Mascarpone UnitPrice 10 33 Example

Gorgonzola

16

FORMAT()
The FORMAT() function is used to format how a field is to be displayed. Syntax SELECT FORMAT (column_name, format) FROM table_name ; Example SELECT FORMAT (DOB, 'YYYY-MM-DD') as PerDate FROM Products

ProductName UnitPrice PerDate

Example

Jarlsberg
Mascarpone Gorgonzola

10.45
32.56 15.67

2008-10-07
2008-10-07 2008-10-07

DATA CONTROL LANGUAGE (DCL)

A Data Control Language (DCL) is a computer language and a subset of SQL, used to control access to data in a database.

DCL commands 1. GRANT to allow specified users to perform specified tasks. 2. REVOKE to cancel previously granted or denied permissions.

SELECT : GROUP BY & HAVING

The SQL GROUP BY statement is used together with the SQL aggregate functions to group the

retrieved data by one or more columns.

The SQL HAVING clause is used to restrict conditionally the output of a SQL statement, by a SQL aggregate function used in your SELECT list of columns.

GROUP BY & HAVING


Syntax Select colname,group_function (col_name) from <table_name> [where <condition>] [group by <expression> [having <group_condition>]

EXAMPLE TABLE
O_Id 1 2 3 4 5 OrderDate 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 OrderPrice 1000 1600 700 500 2000 Customer Hansen Nilsen Hansen Hansen Jensen

2008/10/04

100

Nilsen

SQL GROUP BY Example

SELECT Customer, SUM (OrderPrice) FROM Orders GROUP BY Customer

Customer Hansen Nilsen

SUM(OrderPrice) 2200 1700

Jensen

2000

SELECT Customer, SUM (OrderPrice) FROM Orders GROUP BY Customer HAVING SUM (OrderPrice)>=2000

Customer

SUM(OrderPrice)

Hansen
Jensen

2200
2000

Вам также может понравиться