CHAP 3 part 1 k scheme
CHAP 3 part 1 k scheme
CHAP 3 part 1 k scheme
Tuning(part 1)
SQL
o SQL stands for Structured Query Language. It is used for storing and managing data in
relational database management system (RDMS).
o SQL statements are used to perform different operation on database like creation,
insertion, deletion etc.
o All the RDBMS like MYSQL, Informix, Oracle, MS Access and SQL Server use SQL as their
standard database language.
M. U. MUN DMS
Types of SQL Commands
SQL is a structured query language, which is used to deal with structured data. Structured data is data
that is generally stored in the form of relations or tables.
Whenever we store the data in tables or relations, we need SQL commands. Moreover, these
commands are also required to retrieve the data which is stored in tables.
o All the structural changes such as creation, deletion and alteration on the table can be
carried with the DDL commands in SQL.
M. U. MUN DMS
Data Manipulation Language
o DML stands for Data Manipulation Language. Using DML commands in SQL, we can make
changes in the data present in tables.
o Whenever we wish to manipulate the data or fetch the data present in SQL tables, we can
use DML commands in SQL.
o DML commands in SQL will change the data, such as inserting new records, deleting or
updating existing records from the SQL tables. We can also retrieve all the data from SQL
tables according to our requirements.
M. U. MUN DMS
1. GRANT: Access privileges can be assigned to a user for the databases and tables
using the GRANT command.
The Syntax for the GRANT command is:
privilege_name is the access right granted to the user. Some of the access
rights are ALL, EXECUTE, SELECT, UPDATE, DELETE, INSERT etc.
object_name is the name of an database object like TABLE, VIEWand
SEQUENCE.
user_name is the name of the user to whom an access right is being granted.
Example:
2. REVOKE: All the access privileges which are already assigned to the user can be
revoked by using the REVOKE command.
Example:
REVOKE SELECT, UPDATE, INSERT, DELETE ON TABLE_NAME FROM
USERNAME;
o Using TCL commands in SQL, we can save our transactions to the database and roll them
back to a specific point in our transaction. We can also save a particular portion of our
transaction using the SAVEPOINT command.
M. U. MUN DMS
1. COMMIT:
To save all the operations executed in a particular transaction, we need to execute a
commit command just after the transaction completion.
2. ROLLBACK
Using the rollback command in SQL, you can roll to the last saved state of a transaction.
3. SAVEPOINT
Using the SAVEPOINT command, you can assign a name to a specific part of the
transaction.
M. U. MUN DMS
DML COMMANDS
Following are the four main DML commands in SQL:
1. SELECT Command
2. INSERT Command
3. UPDATE Command
4. DELETE Command
o Syntax of INSERT Command (when user want to insert specific column values)
o Suppose, you want to insert a new record into the student table. For this, you
have to write the following DML INSERT command:
M. U. MUN DMS
o Suppose, you want to insert a new record with specific column values into the
student table. For this, you have to write the following DML INSERT command:
o After executing above two queries, student table consist 4 records as follows:
ID NAME MARKS
101 RAM 60
102 SHYAM 80
104 Anmol 89
105 Ankita null
o Here, 'UPDATE', 'SET', and 'WHERE' are the SQL keywords, and 'Table_name' is
the name of the table whose values you want to update.
o Examples of the UPDATE command
o Let's take a Student table consisting of the following records:
ID NAME MARKS
101 RAM 60
102 SHYAM 80
104 Anmol 89
105 Ankita null
M. U. MUN DMS
o Suppose, you want to update Marks of student whose Id is 104. To do this, you have
to write the following DML Update command:
ID NAME MARKS
101 RAM 60
102 SHYAM 80
104 Anmol 90
105 Ankita null
o Suppose, you want to delete the record of those students whose Marks is greater
than 70. To do this, you have to write the following DML Update command:
M. U. MUN DMS
o If we want to retrieve the data from all the columns of the table, we have to
use the following SELECT command:
o Syntax of SELECT DML command
o If we want to retrieve the data from specific columns of the table, we have to
use the following SELECT command:
o This SQL statement displays the following values of the student table:
ID NAME MARKS
101 RAM 60
102 SHYAM 80
104 Anmol 89
105 Ankita null
M. U. MUN DMS
o Example 2: This example shows all the values of a specific column from the
table.
o This SQL statement displays the following values of the student table:
ID NAME
101 RAM
102 SHYAM
104 Anmol
105 Ankita
o Example 3: This example describes how to use the WHERE clause with the SELECT
DML command.
o This SQL statement displays the following values of the student table:
ID NAME MARKS
102 SHYAM 80
104 Anmol 89
M. U. MUN DMS
SQL Operators
An SQL 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.
An SQL operator can be either a unary or binary operator. A unary operator uses
only one operand to perform the unary operation, whereas the binary operator
uses two operands to perform the binary operation.
M. U. MUN DMS
EXAMPLE: LET’S consider following employee table
ID NAME salary
o Suppose, we want to add 20,000 to the salary of each employee specified in the
table. Then, we have to write the following query in the SQL:
ID NAME salary
102 SHYAM 80000
104 Anmol 90000
103 RAM 70000
105 PUJA 60000
M. U. MUN DMS
3. SQL Multiplication Operator (*):The Multiplication Operator in SQL performs
the Multiplication on the numerical data of the database table. In SQL, we can
easily multiply the numerical values of two columns of the same table by
specifying both the column names as the first and second operand.
ID NAME salary
102 SHYAM 80000
104 Anmol 90000
103 RAM 70000
105 PUJA 60000
o Suppose, we want to double the salary of each employee specified in the table.
Then, we have to write the following query in the SQL:
4. SQL Division Operator (/):The Division Operator in SQL divides the operand on
the left side by the operand on the right side.
ID NAME salary
102 SHYAM 80000
o Suppose, we want to half the salary of each employee specified in the table. Then,
we have to write the following query in the SQL:
M. U. MUN DMS
SELECT Salary *2 as Emp_New_Salary FROM Employee;
ID NAME salary
102 SHYAM 80000
104 Anmol 90000
103 RAM 70000
105 PUJA 60000
o Suppose, we want to get the remainder by dividing the VALUE of SALARY column
by the value of ID operand column, Then, we have to write the following query in
the SQL:
SELECT salary%ID as reminder FROM Employee;
M. U. MUN DMS
3. SQL Greater Than Operator (>)
4. SQL Greater Than Equals to Operator (>=)
5. SQL Less Than Operator (<)\
6. SQL Less Than Equals to Operator (<=)
ID NAME salary
102 SHYAM 80000
104 Anmol 90000
103 RAM 70000
105 PUJA 60000
106 jay 80000
M. U. MUN DMS
2. SQL Equal Not Operator (!=)
o The Equal Not Operator in SQL shows only those data that do not match the
query's specified value.
o This operator returns those records or rows from the database views and tables
if the value of both operands specified in the query is not matched with each
other.
o Suppose, we want to access all the records of those employees from
the Employee table whose salary is not 45000. Then, we have to write the
following query in the SQL database:
o In this example, we used the SQL equal not operator with WHERE clause for
getting the records of those employees whose salary is not 45000.
o Here, SQL greater than operator displays the records of those employees from
the above table whose Employee Id is greater than 103.
o Here,'SQL greater than equals to operator' with WHERE clause displays the
rows of those employees from the table whose Employee Id is greater than and
equals to 103.
M. U. MUN DMS
5. SQL Less Than Operator (<)
o The Less Than Operator in SQL shows only those data from the database tables
which are less than the value of the right-side operand.
o This comparison operator checks that the left side operand is lesser than the
right side operand. If the condition becomes true, then this operator in SQL
displays the data which is less than the value of the right-side operand.
o Suppose, we want to access all the records of those employees from
the Employee table whose employee id is less than 104. For this, we have to
write the following query in the SQL database:
o Here,SQL less than operator with WHERE clause displays the records of those
employees from the above table whose Employee Id is less than 104.
o Here, SQL less than equals to the operator with WHERE clause displays the
rows of those employees from the table whose Employee Id is less than and
equals 202.
M. U. MUN DMS
o SQL OR operator
o SQL NOT operator
o SQL BETWEEN operator
o SQL IN operator
o SQL LIKE operator
o Lets Consider Following Employee_details Table For Logical Operations.
o Here, SQL AND operator with WHERE clause shows the record of employees
whose salary is 25000 and the city is Delhi.
2. SQL OR Operator
o The OR operator in SQL shows the record from the table if any of the conditions
separated by the OR operator evaluates to True. It is also known as the
conjunctive operator and is used with the WHERE clause.
M. U. MUN DMS
o Syntax of OR operator:
o Here, SQL AND operator with WHERE clause shows the record of employees
whose salary is 25000 or the city is Delhi.
o In this example, we used the SQL NOT operator with the Emp_City
column.
4. SQL BETWEEN Operator(RANGE SEARCHING OPRATOR)
o The BETWEEN operator in SQL shows the record within the range
mentioned in the SQL query. This operator operates on the numbers,
characters, and date/time operands.
o If there is no value in the given range, then this operator shows NULL
value.
o Syntax of BETWEEN operator:
M. U. MUN DMS
o Suppose, we want to access all the information of those employees from
the Employee_details table who is having salaries between 20000 and 40000.
For this, we have to write the following query in SQL:
o Here, we used the SQL BETWEEN operator with the Emp_Salary field.
5. SQL IN Operator
The IN operator in SQL allows database users to specify two or more
values in a WHERE clause. This logical operator minimizes the
requirement of multiple OR conditions.
This operator makes the query easier to learn and understand. This
operator returns those rows whose values match with any value of the
given list.
Syntax of IN operator:
M. U. MUN DMS
If we want to show all the information of those employees from
the Employee_details whose name starts with ''s''. For this, we have to
write the following query in SQL:
M. U. MUN DMS
1. SQL Union Operator
2. SQL Union ALL Operator
3. SQL Intersect Operator
4. SQL Minus Operator
Let’s Consider Following two tables Emp1 and Emp2 set Operations.
Id Name Salary
201 Sanjay 25000
202 Ajay 45000
203 Saket 30000
Table: Emp1
Id Name Salary
203 Saket 30000
204 Saurabh 40000
205 Ram 30000
201 Sanjay 25000
Table: Emp2
Above query will display the employee name and employee id from both
tables in a single output and exclude duplicate values.
M. U. MUN DMS
Id Name
201 Sanjay
202 Ajay
203 Saket
204 Saurabh
205 Ram
Above query will display the employee name and employee id from both
tables in a single output and include duplicate values.
Id Name
201 Sanjay
202 Ajay
203 Saket
203 Saket
204 Saurabh
205 Ram
201 Sanjay
M. U. MUN DMS
SELECT ID, Name FROM Emp1
INTERSECT
SELECT ID, Name FROM Emp2 ;
Id Name
201 Sanjay
203 Saket
Id Name
202 Ajay
M. U. MUN DMS