DBMS UNIT-4 SQL
DBMS UNIT-4 SQL
Introduction to SQL
• Data types are used to represent the nature of the data that can be
stored in the database table. For example, in a particular column of a
table, if we want to store a string type of data then we will have to
declare a string data type of this column.
• A data type also specifies the possible values for that type, the
operations that can be performed on that type and the way the values of
that type are stored.
• Data types mainly classified into three categories for every database.
1. String Data types
2. Numeric Data types
3. Date and time Data types
Oracle String data types
• The SQL DROP TABLE statement is used to remove a table definition and
all the data, indexes, triggers, constraints and permission specifications
for that table.
• NOTE − You should be very careful while using this command because
once a table is deleted then all the information available in that table will
also be lost forever.
• Syntax : DROP TABLE table_name;
• Example : SQL> DROP TABLE CUSTOMERS;
• Output :Query OK, 0 rows affected (0.01 sec)
SQL - ALTER TABLE Command
• The SQL INSERT INTO Statement is used to add new rows of data to a
table in the database.
• Syntax :
1. INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
2. INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
• Example :
1. INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );
2. INSERT INTO CUSTOMERS VALUES (7, 'Muffy', 24, 'Indore', 10000.00 );
Populate one table using
another table
• You can populate the data into a table through the select statement over
another table; provided the other table has a set of fields, which are
required to populate the first table.
• Syntax :
INSERT INTO first_table_name [(column1, column2, ... columnN)]
SELECT column1, column2, ...columnN FROM second_table_name
[WHERE condition];
Phases of select statement
• The SQL SELECT statement is used to fetch the data from a database
table which returns this data in the form of a result table. These result
tables are called result-sets.
• Syntax :
1. SELECT column1, column2, columnN FROM table_name;
Here, column1, column2... are the fields of a table whose values you
want to fetch. If you want to fetch all the fields available in the field,
then you can use the following syntax.
2. SELECT * FROM table_name;
• Example :
1. SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS;
2. SQL> SELECT * FROM CUSTOMERS;
Optional clauses in SELECT
statement
There are some optional clauses in SELECT statement:
• The SQL UPDATE Query is used to modify the existing records in a table.
You can use the WHERE clause with the UPDATE query to update the
selected rows, otherwise all the rows would be affected.
• Syntax :
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
• Example : The following query will update the ADDRESS for a customer
whose ID number is 6 in the table.
SQL> UPDATE CUSTOMERS
SET ADDRESS = 'Pune'
WHERE ID = 6;
SQL - DELETE Query
• The SQL DELETE Query is used to delete the existing records from a
table.
• You can use the WHERE clause with a DELETE query to delete the
selected rows, otherwise all the records would be deleted.
• Syntax : DELETE FROM table_name WHERE [condition];
• Example : The following code has a query, which will DELETE a
customer, whose ID is 6.
SQL> DELETE FROM CUSTOMERS WHERE ID = 6;
• Note : If you want to DELETE all the records from the CUSTOMERS table,
you do not need to use the WHERE clause and the DELETE query would
be as follows −
SQL> DELETE FROM CUSTOMERS;
Now, the CUSTOMERS table would not have any record.
Logical operators
• The SQL AND & OR operators are used to combine multiple conditions to
narrow data in an SQL statement. These two operators are called as the
conjunctive operators or logical operators.
• These operators provide a means to make multiple comparisons with
different operators in the same SQL statement.
AND Operator
• The AND operator allows the existence of multiple conditions in an SQL
statement's WHERE clause.
• Syntax
• The basic syntax of the AND operator with a WHERE clause is as follows −
SELECT column1, column2, columnN FROM table_name WHERE
[condition1] AND [condition2]...AND [conditionN]; You can combine N
number of conditions using the AND operator. For an action to be taken by
the SQL statement, whether it be a transaction or a query, all conditions
separated by the AND must be TRUE.
• Example : Following is an example, which would fetch the ID, Name and
Salary fields from the CUSTOMERS table, where the salary is greater than
2000 and the age is less than 25 years −
• SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000
AND age < 25;
The OR Operator
• The OR operator is used to combine multiple conditions in an SQL
statement's WHERE clause.
• Syntax
• The basic syntax of the OR operator with a WHERE clause is as follows −
• SELECT column1, column2, columnN FROM table_name WHERE
[condition1] OR [condition2]...OR [conditionN] You can combine N number
of conditions using the OR operator. For an action to be taken by the SQL
statement, whether it be a transaction or query, the only any ONE of the
conditions separated by the OR must be TRUE.
• Example : The following code block has a query, which would fetch the ID,
Name and Salary fields from the CUSTOMERS table, where the salary is
greater than 2000 OR the age is less than 25 years.
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000
OR age < 25;
Range searching in SQL
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any
position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the
second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a"
and are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a"
and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a"
and ends with "o"
•
SQL - Alias
SQL aliases are used to give a table, or a column in a table, a temporary name.
• Aliases are often used to make column names more readable.
• An alias only exists for the duration of the query.
• Syntax
1. The basic syntax of a table alias is as follows.
SELECT column1, column2.... FROM table_name AS alias_name WHERE
[condition];
2. The basic syntax of a column alias is as follows.
SELECT column_name AS alias_name FROM table_name WHERE [condition];
• Example
1. Now, the following code block shows the usage of a table alias.
SQL> SELECT C.ID, C.NAME, C.AGE, O.AMOUNT FROM CUSTOMERS AS C,
ORDERS AS O WHERE C.ID = O.CUSTOMER_ID;
2. Following is the usage of a column alias.
SQL> SELECT ID AS CUSTOMER_ID, NAME AS CUSTOMER_NAME FROM
CUSTOMERS WHERE SALARY IS NOT NULL;
SQL Functions
SQL provides many built-in functions to perform operations on data. These functions are useful while performing mathematical
calculations, string concatenations, sub-strings etc. SQL functions are divided into two categories,
1 Harsh 90 19
2 Suresh 50 20
3 Pratik 80 19
4 Dhanraj 95 21
5 Ram 85 18
• AVG(): It returns average value after calculating from values in a numeric
column.
Syntax: SELECT AVG(column_name) FROM table_name;
Queries: Computing average marks of students.
SELECT AVG(MARKS) AS AvgMarks FROM Students;
Output:
AvgMarks
80
• COUNT(): It is used to count the number of rows returned in a SELECT
statement. Syntax:SELECT COUNT(column_name) FROM table_name;
Queries: Computing total number of students.
SELECT COUNT(*) AS NumStudents FROM Stuents;
Output:
NumStudents
5
• FIRST(): The FIRST() function returns the first value of the selected column.
Syntax:SELECT FIRST(column_name) FROM table_name;
Queries: Fetching marks of first student from the Students table.
SELECT FIRST(MARKS) AS MarksFirst FROM Students;
Output:
MarksFirst
90
• LAST(): The LAST() function returns the last value of the selected column. It
can be used only in MS ACCESS.
Syntax:SELECT LAST(column_name) FROM table_name;
Queries: Fetching marks of last student from the Students table.
SELECT LAST(MARKS) AS MarksLast FROM Students;
Output:
MarksLast
82
• MAX(): The MAX() function returns the maximum value of the selected
column.
Syntax:SELECT MAX(column_name) FROM table_name;
Queries: Fetching maximum marks among students from the Students
table.
SELECT MAX(MARKS) AS MaxMarks FROM Students;
Output:
MaxMarks
95
• MIN(): The MIN() function returns the minimum value of the selected
column.
Syntax:SELECT MIN(column_name) FROM table_name;
Queries: Fetching minimum marks among students from the Students table.
SELECT MIN(MARKS) AS MinMarks FROM Students;
Output:
MinMarks
50
• SUM(): The SUM() function returns the sum of all the values of the selected
column.
Syntax:SELECT SUM(column_name) FROM table_name;
Queries: 1.Fetching summation of total marks among students from the
Students table.
SELECT SUM(MARKS) AS TotalMarks FROM Students;
Output:
TotalMarks
400
2.Fetching summation of total age among students from the Students table.
SELECT SUM(AGE) AS TotalAge FROM Students;
Output:
TotalAge
97
Scalar Functions
SURESH 3 Pratik 80 19
PRATIK 4 Dhanraj 95 21
DHANRAJ 5 Ram 85 18
RAM
• LCASE(): It converts the value of a field to lowercase.
Syntax : SELECT LCASE(column_name) FROM table_name;
suresh 1 Harsh 90 19
2 Suresh 50 20
pratik
3 Pratik 80 19
dhanraj
4 Dhanraj 95 21
ram
5 Ram 85 18
• MID(): The MID() function extracts texts from the text field.
Syntax: SELECT MID(column_name,start,length) AS some_name FROM
table_name;
specifying length is optional here, and start signifies start position ( starting
from 1 )
Queries: Fetching first four characters of names of students from the
Students table.
SELECT MID(NAME,1,4) FROM Students;
Output: NAME
ID NAME MARKS AGE
HARS
1 Harsh 90 19
SURE
2 Suresh 50 20
PRAT
3 Pratik 80 19
DHAN
4 Dhanraj 95 21
RAM
5 Ram 85 18
• LEN(): The LEN() function returns the length of the value in a text field.
Syntax: SELECT LENGTH(column_name) FROM table_name;
Queries: Fetching length of names of students from Students table.
SELECT LENGTH(NAME) FROM Students;
Output: ID NAME MARKS AGE
NAME 1 Harsh 90 19
5 2 Suresh 50 20
6 3 Pratik 80 19
6 4 Dhanraj 95 21
7 5 Ram 85 18
3
• ROUND(): The ROUND() function is used to round a numeric field to the
number of decimals specified.
NOTE: Many database systems have adopted the IEEE 754 standard for
arithmetic operations, which says that when any numeric .5 is rounded it
results to the nearest even integer i.e, 5.5 and 6.5 both gets rounded off to
6.Syntax:
SELECT ROUND(column_name,decimals) FROM table_name;
decimals- number of decimals to be fetched.
Queries: Fetching maximum marks among students from the Students
table.
SELECT ROUND(MARKS,0) FROM table_name;
Output:
ID NAME MARKS AGE
MARKS 1 Harsh 90 19
90 2 Suresh 50 20
50 3 Pratik 80 19
80 4 Dhanraj 95 21
95 5 Ram 85 18
85
• NOW(): The NOW() function returns the current system date and time.
Syntax:SELECT NOW() FROM table_name;
Queries: Fetching current system time.
SELECT NAME, NOW() AS DateTime FROM Students;
Output:
NAME DateTime ID NAME MARKS AGE
HARSH 16/01/2021 1:30:11 PM 1 Harsh 90 19
SURESH 16/01/2021 1:30:11 PM 2 Suresh 50 20
5 Ram 85 18
Transaction Control
The following commands are used to control transactions.