DBMS Unit 4 Notes PDF
DBMS Unit 4 Notes PDF
Basic SQL Query: Basic SQL querying (select and project) using where clause,
arithmetic & logical operations, Set ,Comparison Operators, NULL values ,
Comparison using Null values, sub queries, grouping, aggregation, ordering,
implementation of different types of joins, Simple Database schema, data types,
table definitions, different types of DML and DDL operations
SQL
o SQL stands for Structured Query Language. It is used for storing and managing
data in relational database management system (RDMS).
o It is a standard language for Relational Database System. It enables a user to
create, read, update and delete relational databases and tables.
o All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL
as their standard database language.
o SQL allows users to query the database in a number of ways, using English-like
statements.
Rules:
SQL follows the following rules:
o Structure query language is not case sensitive. Generally, keywords of SQL are
written in uppercase.
o Statements of SQL are dependent on text lines. We can use a single SQL
statement on one or multiple text line.
o Using the SQL statements, you can perform most of the actions in a database.
o SQL depends on tuple relational calculus and relational algebra.
SQL process:
o When an SQL command is executing for any RDBMS, then the system figure out
the best way to carry out the request and the SQL engine determines that how
to interpret the task.
o In the process, various components are included. These components can be
optimization Engine, Query engine, Query dispatcher, classic, etc.
o All the non-SQL queries are handled by the classic query engine, but SQL query
engine won't handle logical files.
1
Characteristics of SQL
o SQL is easy to learn.
o SQL is used to access data from relational database management systems.
o SQL can execute queries against the database.
o SQL is used to describe the data.
o SQL is used to define the data in the database and manipulate it when needed.
o SQL is used to create and drop the database and table.
o SQL is used to create a view, stored procedure, function in a database.
o SQL allows users to set permissions on tables, procedures, and views.
Advantages of SQL
There are the following advantages of SQL:
High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large amount of
records from a database.
No coding needed
In the standard SQL, it is very easy to manage the database system. It doesn't require
a substantial amount of code to manage the database system.
Portability
SQL can be used in laptop, PCs, server and even some mobile phones.
Interactive language
SQL is a domain language used to communicate with the database. It is also used to
receive answers to the complex questions in seconds.
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.
3
o All the command of DDL are auto-committed that means it permanently save all
the changes in the database.
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
Example:
CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), DOB DATE);
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax
DROP TABLE ;
Example
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:
EXAMPLE
ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
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:
TRUNCATE TABLE table_name;
4
Example:
TRUNCATE TABLE EMPLOYEE;
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row
of a table.
Syntax:
INSERT INTO TABLE_NAME(col1, col2, col3,.... col N) VALUES (value1, value2, value3,
.... valueN);
Or
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
For example:
INSERT INTO STUDENT VALUES(1,'RAMESH',24,'RJY',20000);
b. UPDATE: This command is used to update or modify the value of a column in the
table.
Syntax:
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHER
E CONDITION]
For example:
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:
DELETE FROM table_name [WHERE condition];
For example:
DELETE FROM javatpoint WHERE Author="Sonoo";
Example
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
Example
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
These operations are automatically committed in the database that's why they cannot
be used while creating tables or dropping them.
a. Commit: Commit command is used to save all the transactions to the database.
Syntax:
COMMIT;
Example:
DELETE FROM CUSTOMERS WHERE AGE = 25;
COMMIT;
b. Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.
Syntax:
ROLLBACK;
Example:
DELETE FROM CUSTOMERS WHERE AGE = 25;
ROLLBACK;
6
c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling
back the entire transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
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:
SELECT expressions FROM TABLES WHERE conditions;
For example:
SELECT emp_name FROM employee WHERE age > 20;
SQL Operator
There are various types of SQL operator:
7
Operator Description Example
10+20
30
44170288286817 4 44183034646933
21606912080381 21736625668766
10-20
-10
10*20
200
20/10
2
8
SELECT MOD(20,3) FROM DUAL;
MOD(20,3)
2
Operator Description
BETWEEN It is used to search for values that are within a set of values.
9
+----+----------+-----+-----------+----------+
| 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 |
+----+----------+-----+-----------+----------+
7 rows in set (0.00 sec)
Here are some simple examples showing usage of SQL Comparison
Operators −
Example 1
SQL> SELECT * FROM CUSTOMERS WHERE AGE >= 25 AND SALARY >= 6500;
Output
+----+----------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+---------+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
+----+----------+-----+---------+---------+
2 rows in set (0.00 sec)
Example 2
SQL> SELECT * FROM CUSTOMERS WHERE AGE >= 25 OR SALARY >= 6500;
Output
+----+----------+-----+-----------+----------+
| 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 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
5 rows in set (0.00 sec)
Example 3
Output
+----+----------+-----+-----------+----------+
| 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 |
10
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
7 rows in set (0.00 sec)
Example 4
Output
+----+-------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+-------+-----+---------+---------+
| 6 | Komal | 22 | MP | 4500.00 |
+----+-------+-----+---------+---------+
1 row in set (0.00 sec)
Example 5
Output
+----+----------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+---------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
+----+----------+-----+---------+---------+
3 rows in set (0.00 sec)
Example 6
Output
+----+----------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+---------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
+----+----------+-----+---------+---------+
3 rows in set (0.00 sec)
Example 7
WHERE EXISTS (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);
Output
+-----+
11
| AGE |
+-----+
| 32 |
| 25 |
| 23 |
| 25 |
| 27 |
| 22 |
| 24 |
+-----+
7 rows in set (0.02 sec)
Example 8
WHERE AGE > ALL (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);
Output
+----+--------+-----+-----------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+--------+-----+-----------+---------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+--------+-----+-----------+---------+
1 row in set (0.02 sec)
Example 9
WHERE AGE > ANY (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);
Output
+----+----------+-----+-----------+---------+
| 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)
12
3. Intersect
4. Minus
1. Union
o The SQL Union operation is used to combine the result of two or more SQL
SELECT queries.
o In the union operation, all the number of datatype and columns must be same in
both the tables on which UNION operation is being applied.
o The union operation eliminates the duplicate rows from its resultset.
Syntax
Example:
ID NAME
1 Jack
2 Harry
3 Jackson
13
The Second table
ID NAME
3 Jackson
4 Stephan
5 David
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
2. Union All
Union All operation is equal to the Union operation. It returns the set without removing
duplication and sorting the data.
Syntax:
SELECT column_name FROM table1 UNION ALL SELECT column_name FROM table2;
14
Example: Using the above First and Second table.
ID NAME
1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David
3. Intersect
o It is used to combine two SELECT statements. The Intersect operation returns
the common rows from both the SELECT statements.
o In the Intersect operation, the number of datatype and columns must be the
same.
o It has no duplicates and it arranges the data in ascending order by default.
Syntax
Example:
15
ID NAME
3 Jackson
4. Minus
o It combines the result of two SELECT statements. Minus operator is used to
display the rows which are present in the first query but absent in the second
query.
o It has no duplicates and data arranged in ascending order by default.
Syntax:
Example
ID NAME
1 Jack
2 Harry
16
!= It checks if two operands values are equal or not, if (a!=b) is
values are not equal, then condition becomes true. true
> It checks if the left operand value is greater than right (a>b) is
operand value, if yes then condition becomes true. not true
< It checks if the left operand value is less than right (a<b) is
operand value, if yes then condition becomes true. true
>= It checks if the left operand value is greater than or equal (a>=b) is
to the right operand value, if yes then condition becomes not true
true.
<= It checks if the left operand value is less than or equal to (a<=b) is
the right operand value, if yes then condition becomes true
true.
!< It checks if the left operand value is not less than the (a!=b) is
right operand value, if yes then condition becomes true. not true
!> It checks if the left operand value is not greater than the (a!>b) is
right operand value, if yes then condition becomes true. true
Here are some simple examples showing the usage of SQL Comparison Operators
−
Example 1
Output
+----+----------+-----+---------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+----------+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+---------+----------+
3 rows in set (0.00 sec)
Example 2
Output
+----+---------+-----+-----------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+---------+-----+-----------+---------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
+----+---------+-----+-----------+---------+
2 rows in set (0.00 sec)
Example 3
Output
+----+----------+-----+---------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+----------+
| 2 | Khilan | 25 | Delhi | 1500.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 |
+----+----------+-----+---------+----------+
5 rows in set (0.00 sec)
Example 4
Output
+----+----------+-----+---------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+----------+
18
| 2 | Khilan | 25 | Delhi | 1500.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 |
+----+----------+-----+---------+----------+
5 rows in set (0.00 sec)
Example 5
Output
+----+----------+-----+---------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+----------+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+---------+----------+
3 rows in set (0.00 sec)
44170288286817
44183034646933
SELECT * FROM S
21606912080381
21736625668766
19
2 MANIKANTA 27 HYDERBAD 7000
4 SAIKIRAN 26 VIZAG 9000
NULL VALUE:
The SQL NULL is the term used to represent a missing value. A NULL value in a table is a
value in a field that appears to be blank.
A field with a NULL value is a field with no value. It is very important to understand that a
NULL value is different than a zero value or a field that contains spaces.
Syntax
The basic syntax of NULL while creating a table.
Here, NOT NULL signifies that column should always accept an explicit value of the given
data type. There are two columns where we did not use NOT NULL, which means these
columns could be NULL.
A field with a NULL value is the one that has been left blank during the record creation.
Example
The NULL value can cause problems when selecting data. However, because when
comparing an unknown value to any other value, the result is always unknown and not
included in the results. You must use the IS NULL or IS NOT NULL operators to check for
a NULL value.
Consider the following CUSTOMERS table having the records as shown below.
+----+----------+-----+-----------+----------+
20
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | |
| 7 | Muffy | 24 | Indore | |
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| 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 |
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | |
| 7 | Muffy | 24 | Indore | |
+----+----------+-----+-----------+----------+
Important Rule:
21
o A subquery can be placed in a number of SQL clauses like WHERE clause, FROM
clause, HAVING clause.
o You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
o A subquery is a query within another query. The outer query is known as the
main query, and the inner query is known as a subquery.
o Subqueries are on the right side of the comparison operator.
o A subquery is enclosed in parentheses.
o In the Subquery, ORDER BY command cannot be used. But GROUP BY command
can be used to perform the same function as ORDER BY command.
Syntax
Example
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
22
SELECT * FROM EMPLOYEE WHERE ID IN (SELECT ID FROM EMPLOYEEWHERE
SALARY > 4500);
4 Alina 29 UK 6500.00
Syntax:
Example
Now use the following syntax to copy the complete EMPLOYEE table into the
EMPLOYEE_BKP table.
Syntax
23
UPDATE table SET column_name = new_value WHERE VALUE OPERATOR(SELECT COLU
MN_NAME FROM TABLE_NAME WHERE condition);
Example
This would impact three rows, and finally, the EMPLOYEE table would have the following
records.
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
Syntax
24
TABLE_NAME WHERE condition);
Example
DELETE FROM EMPLOYEE WHERE AGE IN (SELECT AGE FROM EMPLOYEE_BKP WHERE
AGE >= 29 );
This would impact three rows, and finally, the EMPLOYEE table would have the following
records.
1 John 20 US 2000.00
Views in SQL
o Views in SQL are considered as a virtual table. A view also contains rows and
columns.
o To create the view, we can select the fields from one or more tables present in
the database.
o A view can either have specific rows based on certain condition or all the rows of
a table.
Sample table:
Student_Detail
25
STU_ID NAME ADDRESS
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Student_Marks
1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
1. Creating view
A view can be created using the CREATE VIEW statement. We can create a view from
a single table or multiple tables.
Syntax:
CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name
WHERE condition;
Query:
CREATE VIEW DetailsView AS SELECT NAME, ADDRESS FROM Student_Details
26
WHERE STU_ID < 4;
Just like table query, we can query the view to view the data.
SELECT * FROM DetailsView;
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
In the given example, a view is created named MarksView from two tables
Student_Detail and Student_Marks.
Query:
Stephan Delhi 97
Kathrin Noida 86
27
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
A view can be deleted using the Drop View statement.
Syntax
DROP VIEW view_name;
Example:
If we want to delete the View MarksView, we can do this as:
DROP VIEW MarksView;
SQL Index
o Indexes are special lookup tables. It is used to retrieve data from the database
very fast.
o An Index is used to speed up select queries and where clauses. But it shows
down the data input with insert and update statements. Indexes can be created
or dropped without affecting the data.
o An index in a database is just like an index in the back of a book.
o For example: When you reference all pages in a book that discusses a certain
topic, you first have to refer to the index, which alphabetically lists all the topics
and then referred to one or more specific page numbers.
Syntax
CREATE INDEX index_name ON table_name (column1, column2, ...);
Example
CREATE INDEX idx_name ON Persons (LastName, FirstName);
Syntax
28
CREATE UNIQUE INDEX index_name ON table_name (column1, column2, ...);
Example
CREATE UNIQUE INDEX websites_idx ON websites (site_name);
Syntax
DROP INDEX index_name;
Example
DROP INDEX websites_idx;
SQL Clauses
The following are the various SQL clauses:
1. GROUP BY
o SQL GROUP BY statement is used to arrange identical data into groups. The
GROUP BY statement is used with the SQL SELECT statement.
o The GROUP BY statement follows the WHERE clause in a SELECT statement and
precedes the ORDER BY clause.
o The GROUP BY statement is used with aggregation function.
Syntax
Sample table:
29
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example:
Output:
Com1 5
Com2 3
Com3 2
2. HAVING
o HAVING clause is used to specify a search condition for a group or an aggregate.
o Having is used in a GROUP BY clause. If you are not using GROUP BY clause then
you can use HAVING function like a WHERE clause.
Syntax:
30
Example:
Output:
Com1 5
Com2 3
3. ORDER BY
o The ORDER BY clause sorts the result-set in ascending or descending order.
o It sorts the records in ascending order by default. DESC keyword is used to sort
the records in descending order.
Syntax:
Where
CUSTOMER
12 Kathrin US
23 David Bangkok
34 Alina Dubai
31
45 John UK
56 Harry US
Output:
34 Alina Dubai
23 David Bangkok
56 Harry US
45 John UK
12 Kathrin US
Output:
12 Kathrin US
32
45 John UK
56 Harry US
23 David Bangkok
34 Alina Dubai
1. COUNT FUNCTION
o COUNT function is used to Count the number of rows in a database table. It can
work on both numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and Null.
Syntax
COUNT(*)
or
33
COUNT( [ALL|DISTINCT] expression )
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example: COUNT()
SELECT COUNT(*) FROM PRODUCT_MAST;
Output:
10
Output:
7
34
Output:
3
Output:
Com1 5
Com2 3
Com3 2
Output:
Com1 5
Com2 3
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric
fields only.
Syntax
SUM()
or
SUM( [ALL|DISTINCT] expression )
Example: SUM()
SELECT SUM(COST) FROM PRODUCT_MAST;
Output:
670
Output:
320
35
SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3 GROUP BY COMPANY;
Output:
Com1 150
Com2 170
Output:
Com1 335
Com3 170
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG
function returns the average of all non-Null values.
Syntax
AVG()
or
AVG( [ALL|DISTINCT] expression )
Example:
SELECT AVG(COST) FROM PRODUCT_MAST;
Output:
67.00
4. MAX Function
MAX function is used to find the maximum value of a certain column. This function
determines the largest value of all selected values of a column.
Syntax
MAX()
or
MAX( [ALL|DISTINCT] expression )
Example:
36
5. MIN Function
MIN function is used to find the minimum value of a certain column. This function
determines the smallest value of all selected values of a column.
Syntax
MIN()
or
MIN( [ALL|DISTINCT] expression )
Example:
Output:
10
SQL JOIN
As the name shows, JOIN means to combine something. In case of SQL, JOIN means
"to combine two or more tables".
In SQL, JOIN clause is used to combine the records from two or more tables in a
database.
SQL Join is used to fetch data from two or more tables, which is joined to appear as
single set of data. SQL Join is used for combining column from two or more tables by
using values common to both tables. Join Keyword is used in SQL queries for joining
two or more tables. Minimum required condition for joining table, is (n-1) where n, is
number of tables. A table can also join to itself known as, Self Join.
ID NAME
1 abhi
2 adam
3 alex
4 anu
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
38
Cross JOIN or Cartesian product
This type of JOIN returns the Cartesian product of rows from the tables in Join. It will
return a table which consists of records which combines each row from the first table
with each row of the second table.
Cross JOIN Syntax is,
ID NAME
1 abhi
2 adam
4 alex
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
ID NAME ID Address
1 abhi 1 DELHI
39
2 adam 1 DELHI
4 alex 1 DELHI
1 abhi 2 MUMBAI
2 adam 2 MUMBAI
4 alex 2 MUMBAI
1 abhi 3 CHENNAI
2 adam 3 CHENNAI
4 alex 3 CHENNAI
Natural JOIN
Natural Join is a type of Inner join which is based on column having same name and
same datatype present in both the tables to be joined.
Natural Join Syntax is,
ID NAME
1 abhi
2 adam
3 alex
40
4 anu
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
ID NAME Address
1 abhi DELHI
2 adam MUMBAI
3 alex CHENNAI
In the above example, both the tables being joined have ID column(same name and
same datatype), hence the records for which value of ID matches in both the tables will
be the result of Natural Join of these two tables.
Outer JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further
into,
41
The left outer join returns a result table with the matched data of two tables then
remaining rows of the left table and null for the right table's column.
Left Outer Join syntax is,
SELECT column-name-list from table-name1 LEFT OUTER JOIN table-name2 on table-
name1.column-name = table-name2.column-name;
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
42
8 PANIPAT
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
ID NAME
1 abhi
43
2 adam
3 alex
4 anu
5 ashish
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
44
null null 7 NOIDA
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
ID Address
1 DELHI
45
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
Database Schema
A database schema is the skeleton structure that represents the logical view of the
entire database. It defines how the data is organized and how the relations among
them are associated. It formulates all the constraints that are to be applied on the
data.
46
A database schema defines its entities and the relationship among them. It
contains a descriptive detail of the database, which can be depicted by means of
schema diagrams. It’s the database designers who design the schema to help
programmers understand the database and make it useful.
• Logical Database Schema − This schema defines all the logical constraints that need
to be applied on the data stored. It defines tables, views, and integrity constraints.
The design of a database at physical level is called physical schema, how the data stored
in blocks of storage is described at this level.
Design of database at logical level is called logical schema, programmers and database
administrators work at this level, at this level data can be described as certain types of data
records gets stored in data structures, however the internal details such as implementation
of data structure is hidden at this level (available at physical level).
Design of database at view level is called view schema. This generally describes end user
interaction with database systems.
Definition of instance: The data stored in database at a particular moment of time is called
instance of database. Database schema defines the variable declarations in tables that
belong to a particular database; the value of these variables at a moment of time is called
the instance of that database.
SQL Datatype
47
o SQL Datatype is used to define the values that a column can contain.
o Every column is required to have a name and data type in the database table.
Datatype of SQL:
1. Binary Datatypes
There are Three types of binary Datatypes which are given below:
48
2.9 etc.
Datatype Description
49
timestamp It stores the year, month, day, hour, minute, and the second
value.
SQL Table
o SQL Table is a collection of data which is organized in terms of rows and
columns. In DBMS, the table is known as relation and row as a tuple.
o Table is a simple form of data storage. A table is also considered as a convenient
representation of relations. Let's see an example of the EMPLOYEE table:
In the above table, "EMPLOYEE" is the table name, "EMP_ID", "EMP_NAME", "CITY",
"PHONE_NO" are the column names. The combination of data of multiple columns
forms a row, e.g., 1, "Kristen", "Washington" and 7289201223 are the data of one row.
Operation on Table
1. Create table
2. Drop table
3. Delete table
4. Rename table
Syntax
50
create table customers(id int,name varchar(100),age int,address varchar(100),salary
int);
Example
Table created.
If you create the table successfully, you can verify the table by looking at the
message by the SQL server. Else you can use DESC command as follows:
Now you have an EMPLOYEE table in the database, and you can use the stored
information related to the employees.
Drop table
A SQL drop table is used to delete a table definition and all the data from a table. When
this command is executed, all the information available in the table is lost forever, so
you have to very careful while using this command.
Syntax
DROP TABLE "table_name";
Firstly, you need to verify the EMPLOYEE table using the following command:
SQL> DESC EMPLOYEE;
51
o 4 rows in set (0.35 sec)
This table shows that EMPLOYEE table is available in the database, so we can drop it as
follows:
Now, we can check whether the table exists or not using the following command:
Query OK, 0 rows affected (0.01 sec)
Syntax
DELETE FROM table_name WHERE condition;
Example
52
1 Kristen Chicago 9737287378 150000
If you don't specify the WHERE condition, it will remove all the rows from the table.
Syntax
SELECT column1, column2, ... FROM table_name;
Here, the expression is the field name of the table that you want to select data from.
Use the following syntax to select all the fields available in the table:
SELECT * FROM table_name;
Example:
EMPLOYEE
53
To fetch the EMP_ID of all the employees, use the following query:
SELECT EMP_ID FROM EMPLOYEE;
Output
EMP_ID
EMP_NAME SALARY
Kristen 150000
Russell 200000
Angelina 600000
Robert 350000
Christian 260000
To fetch all the fields from the EMPLOYEE table, use the following query:
Output
54
4 Robert Washington 9367238263 350000
Sample Table
EMPLOYEE
Syntax
INSERT INTO TABLE_NAME VALUES (value1, value2, value 3, .... Value N);
Query
INSERT INTO EMPLOYEE VALUES (6, 'Marry', 'Canada', 600000, 48);
Output: After executing this query, the EMPLOYEE table will look like:
55
2 Robert Austin 300000 26
Syntax
INSERT INTO TABLE_NAME[(col1, col2, col3,.... col N)] VALUES (value1, value2, value
3, .... Value N);
Query
INSERT INTO EMPLOYEE (EMP_ID, EMP_NAME, AGE) VALUES (7, 'Jack', 40);
Output: After executing this query, the table will look like:
56
Syntax
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Sample Table
EMPLOYEE
Syntax
UPDATE table_name SET column_name = value WHERE condition;
Query
UPDATE EMPLOYEE SET EMP_NAME = 'Emma' WHERE SALARY = 500000;
Output: After executing this query, the EMPLOYEE table will look like:
57
4 Emma Washington 500000 29
Syntax
UPDATE table_name SET column_name = value1, column_name2 = value2 WHERE
condition;
Query
UPDATE EMPLOYEE SET EMP_NAME = 'Kevin', City = 'Boston' WHERE EMP_ID = 5;
Output
Syntax
58
UPDATE table_name SET column_name = value1;
Query
UPDATE EMPLOYEE SET EMP_NAME = 'Harry';
Output
Syntax
DELETE FROM table_name WHERE some_condition;
Sample Table
EMPLOYEE
59
4 Kristen Washington 500000 29
Query
DELETE FROM EMPLOYEE WHERE EMP_NAME = 'Kristen';
Output: After executing this query, the EMPLOYEE table will look like:
Output: After executing this query, the EMPLOYEE table will look like:
60
5 Russell Los angels 200000 36
https://www.javatpoint.com/dbms-sql-set-operation
61