Unit Ii
Unit Ii
o SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
tinyint 0 255
Bit 0 1
1 Char
Maximum length of 8,000 characters.( Fixed length non-Unicode
characters)
2 Varchar
Maximum of 8,000 characters.(Variable-length non-Unicode data).
varchar(max)
Text
o CREATE
o ALTER
o DROP
o TRUNCATE
Syntax:
Example:
1. CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(10
0), DOB DATE);
b. DROP: It is used to delete both the structure and record stored in the
table.
Syntax
Example
Syntax:
EXAMPLE
d. TRUNCATE: It is used to delete all the rows from the table and free the
space containing the table.
Syntax:
Example:
o INSERT
o UPDATE
o DELETE
Syntax:
Or
For example:
Syntax:
Syntax:
For example:
o Grant
o Revoke
Example
Example
o COMMIT
o ROLLBACK
o SAVEPOINT
Syntax:
1. COMMIT;
Example:
Syntax:
1. ROLLBACK;
Example:
Syntax:
1. SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language
DQL is used to fetch the data from the database.
o SELECT
Syntax:
For example:
Operator in SQL
An operator is a reserved word or a character used primarily in an SQL
statement's WHERE clause to perform operation(s), such as comparisons
and arithmetic operations. These Operators are used to specify conditions in
an SQL statement and to serve as conjunctions for multiple conditions in a
statement.
Arithmetic operators
Comparison operators
Logical operators
Show Examples
Operator Description Example
+ (Addition) Adds values on either side of a + b will
the operator. give 30
- (Subtraction) Subtracts right hand operand a - b will
from left hand operand. give -10
* Multiplies values on either a * b will
(Multiplication) side of the operator. give 200
/ (Division) Divides left hand operand by b / a will
right hand operand. give 2
% (Modulus) Divides left hand operand by b % a will
right hand operand and give 0
returns remainder.
Show Examples
Operato Description Example
r
= Checks if the values of two operands are equal or (a = b) is
not, if yes then condition becomes true. not true.
!= Checks if the values of two operands are equal or (a != b) is
not, if values are not equal then condition true.
becomes true.
<> Checks if the values of two operands are equal or (a <> b) is
not, if values are not equal then condition true.
becomes true.
> Checks if the value of left operand is greater than (a > b) is
the value of right operand, if yes then condition not true.
becomes true.
< Checks if the value of left operand is less than the (a < b) is
value of right operand, if yes then condition true.
becomes true.
>= Checks if the value of left operand is greater than (a >= b) is
or equal to the value of right operand, if yes then not true.
condition becomes true.
<= Checks if the value of left operand is less than or (a <= b) is
equal to the value of right operand, if yes then true.
condition becomes true.
!< Checks if the value of left operand is not less than (a !< b) is
the value of right operand, if yes then condition false.
becomes true.
!> Checks if the value of left operand is not greater (a !> b) is
than the value of right operand, if yes then true.
condition becomes true.
The SQL WHERE clause is used to specify a condition while fetching the
data from a single table or by joining with multiple tables. If the given
condition is satisfied, then only it returns a specific value from the table.
You should use the WHERE clause to filter the records and fetching only the
necessary records.
The WHERE clause is not only used in the SELECT statement, but it is also
used in the UPDATE, DELETE statement, etc., which we would examine in
the subsequent chapters.
Syntax
The basic syntax of the SELECT statement with the WHERE clause is as
shown below.
SELECT column1, column2, columnNFROMtable_nameWHERE [condition]
You can specify a condition using the comparison or logical operators like >,
<, =, LIKE, NOT, etc. The following examples would make this concept
clear.
Example
Consider the CUSTOMERS table having the following records −
+-----+-----------+-----+------------+------------+
| 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 |
+-----+----------+------+------------+------------+
The following code is an example which would fetch the ID, Name and
Salary fields from the CUSTOMERS table, where the salary is greater than
2000 −
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY >2000;
This would produce the following result −
+----+----------+-------------+
| ID | NAME | SALARY |
+----+----------+----------+
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+----------+----------+
The following query is an example, which would fetch the ID, Name and
Salary fields from the CUSTOMERS table for a customer with the
name Hardik.
Here, it is important to note that all the strings should be given inside single quotes ('').
Whereas, numeric values should be given without any quote as in the above example.
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE NAME ='Hardik';
ORDER BY Clause
The SQL ORDER BY clause is used to sort the data in ascending or
descending order, based on one or more columns. Some databases sort the
query results in an ascending order by default.
Syntax
The basic syntax of the ORDER BY clause is as follows −
SELECT column-list FROM table_name [WHERE condition]
[ORDER BY column1, column2, ..columnN] [ASC | DESC];
You can use more than one column in the ORDER BY clause. Make sure
whatever column you are using to sort that column should be in the
column-list.
Example
Consider the CUSTOMERS table having the following records −
+----+------------+-----+-----------+-------------+
| 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 |
The following code block has an example, which would sort the result in an
ascending order by the NAME and the SALARY −
The following code block has an example, which would sort the result in the
descending order by NAME.
Syntax
The basic syntax of a GROUP BY clause is shown in the following code block.
The GROUP BY clause must follow the conditions in the WHERE clause and
must precede the ORDER BY clause if one is used.
SELECT column1, column2FROM table_nameWHERE [ conditions ]GROUP BY column1,
column2ORDER BY column1, column2;
Example
Consider the CUSTOMERS table is having the following records −
+----+------------+-----+-------------+-----------+
| 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 |
+----+------------+-----+-------------+-----------+
If you want to know the total amount of the salary on each customer, then
the GROUP BY query would be as follows.
+----+----------+------+------------+-----------+
|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 |
+----+-----------+-----+------------+------------+
Now again, if you want to know the total amount of salary on each
customer, then the GROUP BY query would be as follows −
Having Clause
The HAVING Clause enables you to specify conditions that filter which
group results appear in the results.
The WHERE clause places conditions on the selected columns, whereas the
HAVING clause places conditions on groups created by the GROUP BY
clause.
Syntax
The following code block shows the position of the HAVING Clause in a
query.
SELECT FROM WHERE GROUP BY HAVING ORDER BY
The HAVING clause must follow the GROUP BY clause in a query and must
also precede the ORDER BY clause if used. The following code block has the
syntax of the SELECT statement including the HAVING clause −
Example
+----+----------+------+------------+-----------+
|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 |
+----+-----------+-----+------------+------------+
Aggregate functions in DBMS take multiple rows from the table and return a value
according to the query.
All the aggregate functions are used in Select statement.
Syntax:
COUNT Function
SQL COUNT function is the simplest function and very useful in counting
the number of records, which are expected to be returned by a SELECT
statement.
Now suppose based on the above table you want to count total number of
rows in this table, then you can do it as follows –
MAX Function
SQL MAX function is used to find out the record with maximum value
among a record set.
Now suppose based on the above table you want to fetch maximum value of
daily_typing_pages, then you can do so simply using the following
command −
SQL>SELECT MAX(daily_typing_pages)
-> FROM employee_tbl;
+-------------------------+
|MAX(daily_typing_pages)|
+-----------------------+
|350 |
+-------------------------+
1 row inset(0.00 sec)
You can find all the records with maxmimum value for each name
using GROUP BY clause as follows −
You can use MIN Function along with MAX function to find out minimum
value as well. Try out the following example −
MIN Function
SQL MIN function is used to find out the record with minimum value among
a record set.
Now suppose based on the above table you want to fetch minimum value of
daily_typing_pages, then you can do so simply using the following
command −
You can find all the records with minimum value for each name
using GROUP BY clause as follows −
You can use MIN Function along with MAX function to find out minimum
value as well. Try out the following example −
Now suppose based on the above table you want to calculate average of all
the dialy_typing_pages, then you can do so by using the following
command −
You can take average of various records set using GROUP BYclause.
Following example will take average all the records related to a single
person and you will have average typed pages by every person.
SUM Function
SQL SUM function is used to find out the sum of a field in various records.
Now suppose based on the above table you want to calculate total of all the
dialy_typing_pages, then you can do so by using the following command −
You can take sum of various records set using GROUP BYclause. Following
example will sum up all the records related to a single person and you will
have total typed pages by every person.
A view contains rows and columns, just like a real table. The fields in a
view are fields from one or more real tables in the database.
You can add SQL statements and functions to a view and present the data
as if the data were coming from one single table.
Syntax
Example
CREATE VIEW NEW_Customers AS SELECT CustomerName, ContactName
FROM Customers
WHERE City = 'Kolhapur';
Indexes are used to retrieve data from the database more quickly than otherwise. The
users cannot see the indexes, they are just used to speed up searches/queries.
SYNTAX
SEQUENCES
Sequence is a set of integers 1, 2, 3, … that are generated and
supported by some database systems to produce unique values on
demand.
A sequence is a user defined schema bound object that
generates a sequence of numeric values.
Sequences are frequently used in many databases because
many applications require each row in a table to contain a unique
value and sequences provides an easy way to generate them.
The sequence of numeric values is generated in an ascending
or descending order at defined intervals and can be configured to
restart when exceeds max_value.
Syntax
CREATE SEQUENCE sequence_name
START WITH initial_value
INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
CYCLE|NOCYCLE ;
EXample
CREATE SEQUENCE sequence_1
start with 1
increment by 1
minvalue 0
maxvalue 100
cycle;
Example to use sequence : create a table named students with columns as id and name.
CREATE TABLE students
( ID number(10), NAME char(20) );
Now insert values into table
INSERT into students VALUES(sequence_1.nextval,'Ramesh');
INSERT into students VALUES(sequence_1.nextval,'Suresh');
MySQL Subquery
A subquery in MySQL is a query, which is nested into another SQL
query and embedded with SELECT, INSERT, UPDATE or DELETE
statement along with the various operators. We can also nest the
subquery with another subquery. A subquery is known as the inner
query, and the query that contains subquery is known as the outer
query. The inner query executed first gives the result to the outer
query, and then the main/outer query will be
performed. MySQL allows us to use subquery anywhere, but it must
be closed within parenthesis. All subquery forms and operations
supported by the SQL standard will be supported in MySQL also.
o If the main query does not have multiple columns for subquery,
then a subquery can have only one column in the SELECT
command.
o We can use various comparison operators with the subquery,
such as >, <, =, IN, ANY, SOME, and ALL. A multiple-row
operator is very useful when the subquery returns more than
one row.
o
Syntax
SELECT column_name FROM table_name
o WHERE column_name OPERATOR
o (SELECT column_name FROM table_name where
column_name operator condition )
Example