You are on page 1of 11

SQL

SQL is Structured Query Language, which is a computer language for storing,


manipulating and retrieving data stored in relational database.

Features

Allows
Allows
Allows
Allows

compilers.
Allows users to create and drop databases and tables
Allows users to create view, stored procedure, functions in a database.
Allows users to set permissions on tables, procedures and views

users to access data in relational database management systems.


users to describe the data.
users to define the data in database and manipulate that data.
to embed within other languages using SQL modules, libraries & pre-

SQL COMMANDS
The standard SQL commands to interact with relational databases are CREATE,
SELECT, INSERT, UPDATE, DELETE and DROP. These commands can be classified into
groups based on their nature:

DDL-Data Definition Language


CREATE - Creates a new table, a view of a table, or other object in database
ALTER - Modifies an existing database object, such as a table.
DROP - Deletes an entire table, a view of a table or other object in the database.

DML-Data Manipulation Language


INSERT - Creates a record
UPDATE - Modifies records
DELETE - Deletes records
SELECT - Retrieves certain records from one or more tables

DCL-Data Control Language


GRANT - Gives a privilege to user
REVOKE - Takes back privileges granted from user

Basic syntax of CREATE DATABASE statement is as follows:


CREATE DATABASE DatabaseName;

Basic syntax of USE statement is as follows:


USE DatabaseName;

Basic syntax of DROP DATABASE statement is as follows:


DROP DATABASE DatabaseName;

Create Table with Primary key


create table customers(id numeric not null, name varchar(20) not null,
age numeric not null, address varchar(50), salary decimal(18, 2),
primary key (id));

Create Table with Foreign key


create table orders(orderid numeric not null, order_date datetime,
customer_id numeric references customers(id),amount decimal(18,2),
primary key (orderid));

Create Table with Check Constraint


create table customers(id numeric not null, name varchar(30) not null,
age numeric not null check (age >= 18),address varchar(50),
salary decimal(18, 2), primary key (id));

Basic syntax of DROP TABLE statement is as follows:


DROP TABLE table_name;

Insert
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Ramesh',
32, 'Ahmedabad', 2000.00 );

Update
UPDATE CUSTOMERS SET AGE=35, SALARY=3000.00 WHERE ID=1;

Delete
DELETE FROM CUSTOMERS WHERE ID=1;

Select
SELECT * FROM CUSTOMERS;

Select with condition


SELECT * FROM CUSTOMERS WHERE SALARY>2000;
SELECT * FROM CUSTOMERS WHERE SALARY>2000 AND NAME LIKE 'R%';
SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000 OR AGE < 25;
SELECT ID, NAME, AGE, ADDRESS, SALARY FROM CUSTOMERS WHERE SALARY IS NULL;
SELECT ID, NAME, AGE, ADDRESS, SALARY FROM CUSTOMERS WHERE SALARY IS NOT NULL;

Distinct
SELECT DISTINCT SALARY FROM CUSTOMERS ORDER BY SALARY;

Order By
SELECT * FROM CUSTOMERS ORDER BY NAME, SALARY;
SELECT * FROM CUSTOMERS ORDER BY NAME DESC;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
+----+----------+-----+-----------+----------+

Group By
Let us have following table where CUSTOMERS table has the following records
with duplicate names:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Ramesh | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | kaushik | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

SELECT NAME, SUM(SALARY) FROM CUSTOMERS GROUP BY NAME;


+---------+-------------+
| NAME | SUM(SALARY) |
+---------+-------------+
| Hardik | 8500.00 |
| kaushik | 8500.00 |
| Komal | 4500.00 |
| Muffy | 10000.00 |
| Ramesh | 3500.00 |
+---------+-------------+

Having Clause
SELECT * FROM CUSTOMERS GROUP BY age HAVING COUNT(age) >= 2;

AGGREGATE FUNCTIONS
SELECT
SELECT
SELECT
SELECT
SELECT

COUNT(*) FROM CUSTOMERS


SUM(SALARY) FROM CUSTOMERS
AVG(SALARY) FROM CUSTOMERS
MAX(SALARY) FROM CUSTOMERS
MIN(SALARY) FROM CUSTOMERS

SQL Arithmetic Operators


Assume variable a holds 10 and variable b holds 20, then:
Operator
+
*
/
%

Description
Addition - Adds values on either
side of the operator
Subtraction - Subtracts right hand
operand from left hand operand
Multiplication - Multiplies values
on either side of the operator
Division - Divides left hand
operand by right hand operand
Modulus - Divides left hand
operand by right hand operand
and returns remainder

Example
a + b will give 30
a - b will give -10
a * b will give 200
b / a will give 2
b % a will give 0

SQL Comparison Operators

Assume variable a holds 10 and variable b holds 20, then:


Operator
=
!=

<>

Description
Checks if the values of two
operands are equal or not, if yes
then condition becomes true.
Checks if the values of two
operands are equal or not, if
values are not equal then
condition becomes true.
Checks if the values of two
operands are equal or not, if
values are not equal then
condition becomes true.

Example
(a = b) is not true.
(a != b) is true.

(a <> b) is true.

>

<

>=

<=

!<

!>

Checks if the value of left operand


is greater than the value of right
operand, if yes then condition
becomes true.
Checks if the value of left operand
is less than the value of right
operand, if yes then condition
becomes true.
Checks if the value of left operand
is greater than or equal to the
value of right operand, if yes then
condition becomes true.
Checks if the value of left operand
is less than or equal to the value
of right operand, if yes then
condition becomes true.
Checks if the value of left operand
is not less than the value of right
operand, if yes then condition
becomes true.
Checks if the value of left operand
is not greater than the value of
right operand, if yes then
condition becomes true.

(a > b) is not true.

(a < b) is true.

(a >= b) is not true.

(a <= b) is true.

(a !< b) is false.

(a !> b) is true.

SQL Logical Operators


Operator
ALL
AND
ANY
BETWEEN

EXISTS
IN
LIKE
NOT

Description
The ALL operator is used to compare a
value to all values in another value set.
The AND operator allows the existence
of multiple conditions in an SQL
statement's WHERE clause.
The ANY operator is used to compare a
value to any applicable value in the list
according to the condition.
The BETWEEN operator is used to
search for values that are within a set
of values, given the minimum value
and the maximum value.
The EXISTS operator is used to search
for the presence of a row in a specified
table that meets certain criteria.
The IN operator is used to compare a
value to a list of literal values that have
been specified.
The LIKE operator is used to compare a
value to similar values using wildcard
operators.
The NOT operator reverses the
meaning of the logical operator with
which it is used. Eg: NOT EXISTS, NOT
BETWEEN, NOT IN, etc. This is a
negate operator.

OR
IS NULL
UNIQUE

The OR operator is used to combine


multiple conditions in an SQL
statement's WHERE clause.
The NULL operator is used to compare
a value with a NULL value.
The UNIQUE operator searches every
row of a specified table for uniqueness
(no duplicates).

SELECT * FROM CUSTOMERS WHERE NAME LIKE 'Ko%';


+----+-------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+-------+-----+---------+---------+
| 6 | Komal | 22 | MP | 4500.00 |
+----+-------+-----+---------+---------+
1 row in set (0.00 sec)
SELECT * FROM CUSTOMERS WHERE AGE IN ( 25, 27 );
+----+----------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+---------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
+----+----------+-----+---------+---------+
3 rows in set (0.00 sec)
SELECT * FROM CUSTOMERS WHERE AGE BETWEEN 25 AND 27;
+----+----------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+---------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
+----+----------+-----+---------+---------+
3 rows in set (0.00 sec)
SELECT AGE FROM CUSTOMERS
WHERE EXISTS (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);
+-----+
| AGE |
+-----+
| 32 |
| 25 |
| 23 |
| 25 |
| 27 |
| 22 |
| 24 |
+-----+
7 rows in set (0.02 sec)
SELECT * FROM CUSTOMERS
WHERE AGE > ALL (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);
+----+--------+-----+-----------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+--------+-----+-----------+---------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+--------+-----+-----------+---------+
1 row in set (0.02 sec)
SELECT * FROM CUSTOMERS
WHERE AGE > ANY (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);

+----+----------+-----+-----------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+---------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
4 rows in set (0.00 sec)

SUBQUERIES IN SQL
A Subquery is a query within another SQL query and embedded within the
WHERE clause. A subquery is used to return data that will be used in the main
query as a condition to further restrict the data to be retrieved. Subqueries can
be used with the SELECT, INSERT, UPDATE, and DELETE statements along with
the operators like =, <, >, >=, <=, IN, BETWEEN etc.
SELECT * FROM CUSTOMERS
WHERE SALARY = (SELECT MAX(SALARY) FROM CUSTOMERS);
SELECT * FROM CUSTOMERS WHERE ID IN (SELECT ID FROM ORDERS WHERE
ORDER_DATE='04/05/2014');

JOINS IN SQL

INNER JOIN: returns rows when there is a match in both tables.


LEFT JOIN: returns all rows from the left table, even if there are no matches in

the right table.


RIGHT JOIN: returns all rows from the right table, even if there are no matches

in the left table.


FULL JOIN: returns rows when there is a match in one of the table

INNER JOIN
Consider the following two tables, (a) CUSTOMERS table is as follows:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

(b) Another table is ORDERS as follows:

+-----+---------------------+-------------+--------+
| OID | DATE | ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |

+-----+---------------------+-------------+--------+

Now, let us join these two tables using INNER JOIN as follows:
SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS INNER JOIN ORDERS ON
CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

This would produce the following result:


+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+

LEFT OUTER JOIN


SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS LEFT OUTER JOIN ORDERS ON
CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

This would produce the following result:


+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 1 | Ramesh | NULL | NULL |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
+----+----------+--------+---------------------+

RIGHT OUTER JOIN


SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS RIGHT OUTER JOIN ORDERS ON
CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

This would produce the following result:


+------+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+------+----------+--------+---------------------+

FULL OUTER JOIN


SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS FULL OUTER JOIN ORDERS ON
CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

This would produce the following result:


+------+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+

| 1 | Ramesh | NULL | NULL |


| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+------+----------+--------+---------------------+

UNION
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
LEFT OUTER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
UNION
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
RIGHT OUTER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

This would produce the following result:


+------+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+
| 1 | Ramesh | NULL | NULL |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
+------+----------+--------+---------------------+

INTERSECT
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
LEFT OUTER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
INTERSECT
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
RIGHT OUTER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

This would produce the following result:


+------+---------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+---------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Ramesh | 1560 | 2009-11-20 00:00:00 |
| 4 | kaushik | 2060 | 2008-05-20 00:00:00 |
+------+---------+--------+---------------------+

EXCEPT
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
LEFT OUTER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
EXCEPT
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
RIGHT OUTER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

This would produce the following result:


+----+---------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+---------+--------+---------------------+
| 1 | Ramesh | NULL | NULL |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
+----+---------+--------+---------------------+

INDEX
An index in a database is very similar to an index in the back of a book. An index
helps speed up SELECT queries and WHERE clauses.
CREATE INDEX cusidx ON CUSTOMERS(ID);

Index can be dropped by using,


DROP INDEX cusidx;

VIEW
View is nothing more than a SQL statement that is stored in the database with an
associated name. A view is actually a composition of a table in the form of a
predefined SQL query. A view is virtual table can contain all rows of a table or
select rows from a table. A view can be created from one or many tables which
depends on the written SQL query to create a view.
CREATE VIEW CUSTOMERS_VIEW AS SELECT name, age FROM CUSTOMERS;
SELECT * FROM CUSTOMERS_VIEW;
DROP VIEW CUSTOMERS_VIEW;

ALTER TABLE
ALTER TABLE CUSTOMERS ADD SEX char(1);
ALTER TABLE CUSTOMERS ALTER COLUMN SEX char(6);
ALTER TABLE CUSTOMERS DROP SEX;
ALTER TABLE CUSTOMERS ADD CONSTRAINT cuspk PRIMARY KEY(ID);
ALTER TABLE CUSTOMERS DROP CONSTRAINT cuspk;

DELETE COMPLETE TABLE WITH STRUCTURE


TRUNCATE TABLE CUSTOMERS;

You might also like