Академический Документы
Профессиональный Документы
Культура Документы
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.
Features of SQL
SQL
SQL is made of three sublanguages.
DDL is used to
DDL Commands
Create
Table
Alter
Drop
Table
Table
Rename
CREATE TABLE
CREATE TABLE-Syntax
Create table <table name> (colname1 datatype(size), colname2 datatype(size), ..., colnamen datatype(size));
DataTypes
NUMBER CHAR VARCHAR
DATE
ALTER TABLE
Add a column Drop a column Change a column size or data type for a column
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).
RENAME
RENAME - Syntax
RENAME -Example
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
UPDATE
Syntax UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value Example UPDATE emp SET dno=30 WHERE ename=smith;
DELETE
Syntax DELETE FROM <table_name> [WHERE <condition>]; Example DELETE FROM emp WHERE dno=10;
SELECT - Syntax
OPERATORS
Relational Operators
Logical operators
Special Operators
Relational Operators
Logical operators
AND
OR
NOT
Special Operators
IN BETWEEN
LIKE
AGGREGATE FUNCTIONS
AGGREGATE FUNCTIONS
AVG() - Returns the average value COUNT() - Returns the number of rows
AVG() Function
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(*)
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()
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
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
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
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
start
length
Example
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
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
Example
Jarlsberg
Mascarpone Gorgonzola
10.45
32.56 15.67
2008-10-07
2008-10-07 2008-10-07
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.
The SQL GROUP BY statement is used together with the SQL aggregate functions to group the
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.
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
Jensen
2000
SELECT Customer, SUM (OrderPrice) FROM Orders GROUP BY Customer HAVING SUM (OrderPrice)>=2000
Customer
SUM(OrderPrice)
Hansen
Jensen
2200
2000