0% found this document useful (0 votes)
13 views29 pages

Unit Ii

The document provides an overview of SQL commands, including their types such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Transaction Control Language (TCL), and Data Query Language (DQL). It details various SQL data types, operators, and clauses, along with examples of common SQL commands like CREATE, INSERT, UPDATE, and SELECT. Additionally, it explains how to use SQL for data management and retrieval, emphasizing the importance of conditions in queries.

Uploaded by

aman8122002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views29 pages

Unit Ii

The document provides an overview of SQL commands, including their types such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Transaction Control Language (TCL), and Data Query Language (DQL). It details various SQL data types, operators, and clauses, along with examples of common SQL commands like CREATE, INSERT, UPDATE, and SELECT. Additionally, it explains how to use SQL for data management and retrieval, emphasizing the importance of conditions in queries.

Uploaded by

aman8122002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

SQL command

o SQL commands are instructions. It is used to communicate with the database. It is


also used to perform specific tasks, functions, and queries of data.

o SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.

Numeric Data Types

DATA TYPE FROM TO

bigint -9,223,372,036,854,775,808 9,223,372,036,854,775,807

Int -2,147,483,648 2,147,483,647

smallint -32,768 32,767

tinyint 0 255

Bit 0 1

decimal -10^38 +1 10^38 -1

numeric -10^38 +1 10^38 -1

money -922,337,203,685,477.5808 +922,337,203,685,477.5807

smallmone -214,748.3648 +214,748.3647


y
Approximate Numeric Data Types

DATA TYPE FROM TO

float -1.79E + 1.79E +


308 308

real -3.40E + 38 3.40E + 38

Date and Time Data Types

DATA TYPE FROM TO

datetim Jan 1, Dec 31,


e 1753 9999

smalldatetime Jan 1, Jun 6, 2079


1900

date Stores a date like June 30, 1991

time Stores a time of day like 12:30 P.M.

Note − Here, datetime has 3.33 milliseconds accuracy where as


smalldatetime has 1 minute accuracy.
Character Strings Data Types

Sr.No. DATA TYPE & Description

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)

3 Maximum length of 2E + 31 characters, Variable-length non-Unicode


data (SQL Server 2005 only).

Text

4 Variable-length non-Unicode data with a maximum length of


2,147,483,647 characters.

Types of SQL Command:


1. Data definition language (DDL)
o DDL changes the structure of the table like creating a table, deleting a
table, altering a table, etc.

o All the command of DDL are auto-committed that means it


permanently save all the changes in the database.

Here are some commands that come under DDL:

o CREATE

o ALTER

o DROP

o TRUNCATE

a. CREATE It is used to create a new table in the database

Syntax:

1. CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);

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

1. DROP TABLE TABLNAME;

Example

1. DROP TABLE EMPLOYEE;

c. ALTER: It is used to alter the structure of the database. This change


could be either to modify the characteristics of an existing attribute or
probably to add a new attribute.

Syntax:

add a new column in the table

1. ALTER TABLE table_name ADD column_name COLUMN-definition;

To modify existing column in the table:

1. ALTER TABLE MODIFY(COLUMN DEFINITION....);

EXAMPLE

1. ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));


2. ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));

d. TRUNCATE: It is used to delete all the rows from the table and free the
space containing the table.

Syntax:

1. TRUNCATE TABLE table_name;

Example:

1. TRUNCATE TABLE EMPLOYEE;


2. Data Manipulation Language
o DML commands are used to modify the database. It is responsible for
all form of changes in the database.

o The command of DML is not auto-committed that means it can't


permanently save all the changes in the database. They can be
rollback.

Here are some commands that come under DML:

o INSERT

o UPDATE

o DELETE

a. INSERT: The INSERT statement is a SQL query. It is used to insert data


into the row of a table.

Syntax:

1. INSERT INTO TABLE_NAME (col1, col2, col3,.... col N)


2. VALUES (value1, value2, value3, .... valueN);

Or

1. INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... value


N);

For example:

1. INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS");

b. UPDATE: This command is used to update or modify the value of a


column in the table.

Syntax:

1. UPDATE table_name SET [column_name1= value1,...column_nameN =


valueN] [WHERE CONDITION]
For example:

1. UPDATE students SET User_Name = 'Sonoo' WHERE Student_Id = '3


'

c. DELETE: It is used to remove one or more row from a table.

Syntax:

1. DELETE FROM table_name [WHERE condition];

For example:

1. DELETE FROM javatpoint WHERE Author="Sonoo";

3. Data Control Language


DCL commands are used to grant and take back authority from any database
user.

Here are some commands that come under DCL:

o Grant

o Revoke

a. Grant: It is used to give user access privileges to a database.

Example

1. GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_US


ER;

b. Revoke: It is used to take back permissions from the user.

Example

1. REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;

4. Transaction Control Language


TCL commands can only use with DML commands like INSERT, DELETE and
UPDATE only.
These operations are automatically committed in the database that's why
they cannot be used while creating tables or dropping them.

Here are some commands that come under TCL:

o COMMIT

o ROLLBACK

o SAVEPOINT

a. Commit: Commit command is used to save all the transactions to the


database.

Syntax:

1. COMMIT;

Example:

1. DELETE FROM CUSTOMERS WHERE AGE = 25;


2. COMMIT;

b. Rollback: Rollback command is used to undo transactions that have not


already been saved to the database.

Syntax:

1. ROLLBACK;

Example:

1. DELETE FROM CUSTOMERS WHERE AGE = 25;


2. ROLLBACK;

c. SAVEPOINT: It is used to roll the transaction back to a certain point


without rolling back the entire transaction.

Syntax:

1. SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language
DQL is used to fetch the data from the database.

It uses only one command:

o SELECT

a. SELECT: This is the same as the projection operation of relational


algebra. It is used to select the attribute based on the condition described by
WHERE clause.

Syntax:

1. SELECT expressions FROM TABLES WHERE conditions;

For example:

1. SELECT emp_name FROM employee WHERE age > 20;

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

 Operators used to negate conditions

SQL Arithmetic Operators


Assume 'variable a' holds 10 and 'variable b' holds 20, then −

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.

SQL Comparison Operators


Assume 'variable a' holds 10 and 'variable b' holds 20, then −

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.

SQL Logical Operators


Here is a list of all the logical operators available in SQL.
Show Examples
Sr.No Operator & Description
.
1 ALL-The ALL operator is used to compare a value to all values in
another value set.
2 AND- The AND operator allows the existence of multiple conditions
in an SQL statement's WHERE clause.
3 ANY- The ANY operator is used to compare a value to any
applicable value in the list as per the condition.
4 BETWEEN- The BETWEEN operator is used to search for values
that are within a set of values, given the minimum value and the
maximum value.
5 EXISTS- The EXISTS operator is used to search for the presence of
a row in a specified table that meets a certain criterion.
6 IN- The IN operator is used to compare a value to a list of literal
values that have been specified.
7 LIKE-The LIKE operator is used to compare a value to similar
values using wildcard operators.
8 NOT- 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.
9 OR- The OR operator is used to combine multiple conditions in an
SQL statement's WHERE clause.
10 IS NULL- The NULL operator is used to compare a value with a
NULL value.
11 UNIQUE- The UNIQUE operator searches every row of a specified
table for uniqueness (no duplicates).
SQL Clauses
Where Clauses

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';

This would produce the following result −


+----+----------+-------------+
| ID | NAME | SALARY |
+----+----------+----------+
| 5 | Hardik | 8500.00 |
+----+----------+----------+

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 −

SQL> SELECT * FROM CUSTOMERSORDER BY NAME, SALARY;

This would produce the following result −


+----+-----------+------+-------------+-----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+-----+-----------+------+------------+-----------+

The following code block has an example, which would sort the result in the
descending order by NAME.

SQL> SELECT * FROM CUSTOMERS ORDER BY NAME DESC;

This would produce the following result −


+----+------------+-----+------------+------------+
| 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
The SQL GROUP BY clause is used in collaboration with the SELECT
statement to arrange identical data into groups. This GROUP BY clause
follows the WHERE clause in a SELECT statement and precedes the ORDER
BY clause.

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.

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

This would produce the following result –


+-----------+---------------+
| NAME | SUM(SALARY) |
+----------+-------------+
| Chaitali | 6500.00 |
| Hardik | 8500.00 |
| kaushik | 2000.00 |
| Khilan | 1500.00 |
| Komal | 4500.00 |
| Muffy | 10000.00 |
| Ramesh | 2000.00 |
+-----------+---------------+
Now, let us look at a table where the 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 |
+----+-----------+-----+------------+------------+

Now again, if you want to know the total amount of salary on each
customer, then the GROUP BY query would be as follows −

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

This would produce the following result −


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

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 −

SELECT column1, column2 FROM table1, table2 WHERE [ conditions ] GROUP BY


column1, column2HAVING [ conditions ] ORDER BY column1, column2

Example

Consider the CUSTOMERS table having the following records.

+----+----------+------+------------+-----------+
|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 |
+----+-----------+-----+------------+------------+

Following is an example, which would display a record for a similar age


count that would be more than or equal to 2.

SQL > SELECT ID, NAME, AGE, ADDRESS, SALARYFROM CUSTOMERS

GROUP BY ageHAVING COUNT(age)>=2;

This would produce the following result –


+----+--------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+--------+-----+---------+---------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
+----+--------+-----+---------+---------+
Aggregate Functions

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:

SELECT <FUNCTION NAME>(<PARAMETER>) FROM <TABLE NAME>

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.

To understand COUNT function, consider an employee_tbltable, which is


having the following records −

SQL> SELECT * FROM employee_tbl;


+------+--------+-------------+----------------------+
|id | name |work_date |daily_typing_pages |
+------+------+------------+--------------------+
|1 |John |2007-01-24 |250 |
|2 |Ram |2007-05-27 |220 |
|3 |Jack |2007-05-06 |170 |
|3 |Jack |2007-04-06 |100 |
|4 |Jill |2007-04-06 |220 |
|5 |Zara |2007-06-06 |300 |
|5 |Zara |2007-02-06 |350 |
+-------+-------+-------------+----------------------+

7 rows inset(0.00 sec)

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 –

SQL>SELECT COUNT(*) FROM employee_tbl;


+----------+
|COUNT(*) |
+---------+
|7 |
+----------+
1 row inset(0.01 sec)
Similarly, if you want to count the number of records for Zara, then it can
be done as follows −

SQL>SELECT COUNT(*) FROM employee_tbl


-> WHERE name="Zara";
+-----------+
|COUNT(*) |
+----------+
|2 |
+----------+
1 row inset(0.04

MAX Function
SQL MAX function is used to find out the record with maximum value
among a record set.

To understand MAX function, consider an employee_tbl table, which is


having the following records −

SQL> SELECT * FROM employee_tbl;


+------+--------+-------------+----------------------+
|id | name |work_date |daily_typing_pages |
+------+------+------------+--------------------+
|1 |John |2007-01-24 |250 |
|2 |Ram |2007-05-27 |220 |
|3 |Jack |2007-05-06 |170 |
|3 |Jack |2007-04-06 |100 |
|4 |Jill |2007-04-06 |220 |
|5 |Zara |2007-06-06 |300 |
|5 |Zara |2007-02-06 |350 |
+-------+-------+-------------+----------------------+

7 rows inset(0.00 sec)

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 −

SQL> SELECT id, name,MAX(daily_typing_pages)


-> FROM employee_tbl GROUP BY name;
+------+------+-------------------------+
|id | name | MAX(daily_typing_pages) |
+------+------+-------------------------+
|3 |Jack |170 |
|4 |Jill |220 |
|1 |John |250 |
|2 |Ram |220 |
|5 |Zara |350 |
+------+------+-------------------------+

You can use MIN Function along with MAX function to find out minimum
value as well. Try out the following example −

SQL> SELECT MIN(daily_typing_pages) least, MAX(daily_typing_pages) max


-> FROM employee_tbl;
+-------+------+
|least | max |
+-------+------+
|100 |350 |
+-------+------+
1 row inset(0.01 sec)

MIN Function
SQL MIN function is used to find out the record with minimum value among
a record set.

To understand MIN function, consider an employee_tbl table, which is


having the following records −

SQL> SELECT * FROM employee_tbl;


+------+--------+-------------+----------------------+
|id | name |work_date |daily_typing_pages |
+------+------+------------+--------------------+
|1 |John |2007-01-24 |250 |
|2 |Ram |2007-05-27 |220 |
|3 |Jack |2007-05-06 |170 |
|3 |Jack |2007-04-06 |100 |
|4 |Jill |2007-04-06 |220 |
|5 |Zara |2007-06-06 |300 |
|5 |Zara |2007-02-06 |350 |
+-------+-------+-------------+----------------------+

7 rows inset(0.00 sec)

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 −

SQL> SELECT MIN(daily_typing_pages)


-> FROM employee_tbl;
+-------------------------+
|MIN(daily_typing_pages) |
+-------------------------+
|100 |
+-------------------------+

You can find all the records with minimum value for each name
using GROUP BY clause as follows −

SQL> SELECT id, name,work_date,MIN(daily_typing_pages)


-> FROM employee_tbl GROUP BY name;
+------+------+-------------------------+
|id | name | MIN(daily_typing_pages) |
+------+------+-------------------------+
|3 |Jack |100 |
|4 |Jill |220 |
|1 |John |250 |
|2 |Ram |220 |
|5 |Zara |300 |
+------+------+-------------------------+
5 rows inset(0.00 sec)

You can use MIN Function along with MAX function to find out minimum
value as well. Try out the following example −

SQL> SELECT MIN(daily_typing_pages) least, MAX(daily_typing_pages) max


-> FROM employee_tbl;
+-------+------+
|least | max |
+-------+------+
|100 |350 |
+-------+------+
1 row inset(0.01 sec)
AVG Function
SQL AVG function is used to find out the average of a field in various
records.
To understand AVG function, consider an employee_tbl table, which is
having the following records −
SQL> SELECT * FROM employee_tbl;
+------+--------+-------------+----------------------+
|id | name |work_date |daily_typing_pages |
+------+------+------------+--------------------+
|1 |John |2007-01-24 |250 |
|2 |Ram |2007-05-27 |220 |
|3 |Jack |2007-05-06 |170 |
|3 |Jack |2007-04-06 |100 |
|4 |Jill |2007-04-06 |220 |
|5 |Zara |2007-06-06 |300 |
|5 |Zara |2007-02-06 |350 |
+-------+-------+-------------+----------------------+

7 rows inset(0.00 sec)

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 −

SQL> SELECT AVG(daily_typing_pages)


-> FROM employee_tbl;
+-------------------------+
|AVG(daily_typing_pages) |
+-------------------------+
|230.0000 |
+-------------------------+
1 row inset(0.03 sec)

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.

SQL> SELECT name,AVG(daily_typing_pages) FROM employee_tbl GROUP BY name;


+------+-------------------------+
|name| AVG(daily_typing_pages) |
+------+-------------------------+
|Jack|135.0000 |
|Jill|220.0000 |
|John|250.0000 |
|Ram |220.0000 |
|Zara|325.0000 |
+------+-------------------------+
5 rows inset(0.20 sec)

SUM Function
SQL SUM function is used to find out the sum of a field in various records.

To understand SUM function, consider an employee_tbl table, which is


having the following records −

SQL> SELECT * FROM employee_tbl;


+------+------+------------+--------------------+
|id | name | work_date |daily_typing_pages |
+------+------+------------+--------------------+
|1 |John |2007-01-24 | 250 |
|2 |Ram |2007-05-27 | 220 |
|3 |Jack |2007-05-06 | 170 |
|3 |Jack |2007-04-06 | 100 |
|4 |Jill |2007-04-06 | 220 |
|5 |Zara |2007-06-06 | 300 |
|5 |Zara |2007-02-06 | 350 |
+------+------+------------+--------------------+
7 rows inset(0.00 sec)

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 −

SQL> SELECT SUM(daily_typing_pages)


-> FROM employee_tbl;
+-------------------------+
|SUM(daily_typing_pages) |
+-------------------------+
|1610 |
+-------------------------+
1 row inset(0.00 sec)

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.

SQL> SELECT name,SUM(daily_typing_pages) FROM employee_tbl GROUP BY name;


+------+-------------------------+
|name | SUM(daily_typing_pages) |
+------+-------------------------+
|Jack | 270 |
|Jill | 220 |
|John | 250 |
|Ram | 220 |
|Zara | 650 |
+------+----------------------------+
5 rows inset(0.17 sec)

CREATE VIEW Statement

In SQL, a view is a virtual table based on the result-set of an SQL


statement.

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

CREATE VIEW view_name AS SELECT column1, column2, ...


FROM table_name WHERE condition;

Example
CREATE VIEW NEW_Customers AS SELECT CustomerName, ContactName
FROM Customers
WHERE City = 'Kolhapur';

CREATE INDEX Statement


The CREATE INDEX statement is used to create indexes in tables.

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

CREATE INDEX index_name ON table_name (column1, column2, ...);

CREATE INDEX idx_lastname ON Persons (LastName);

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 ;

sequence_name: Name of the sequence.

initial_value: starting value from where the sequence starts.


Initial_value should be greater than or equal
to minimum value and less than equal to maximum value.

increment_value: Value by which sequence will increment itself.


Increment_value can be positive or negative.

minimum_value: Minimum value of the sequence.


maximum_value: Maximum value of the sequence.

cycle: When sequence reaches its set_limit


it starts from beginning.

nocycle: An exception will be thrown


if sequence exceeds its max_value.

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.

The following are the rules to use subqueries:

o Subqueries should always use in parentheses.

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

1. SELECT emp_name, city, income FROM employees


2. WHERE emp_id IN (SELECT emp_id FROM employees);

You might also like