Professional Documents
Culture Documents
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
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:
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;
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 |
+----+----------+-----+-----------+----------+
Having Clause
SELECT * FROM CUSTOMERS GROUP BY age HAVING COUNT(age) >= 2;
AGGREGATE FUNCTIONS
SELECT
SELECT
SELECT
SELECT
SELECT
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
<>
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.
>
<
>=
<=
!<
!>
(a < b) is true.
(a <= b) is true.
(a !< b) is false.
(a !> b) is true.
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
+----+----------+-----+-----------+---------+
| 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
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 |
+----+----------+-----+-----------+----------+
+-----+---------------------+-------------+--------+
| 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;
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;
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;
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;
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);
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;