0% found this document useful (0 votes)
5 views20 pages

DBMS-UnitIV - Final

This document provides an overview of Structured Query Language (SQL), detailing its purpose in managing relational databases and categorizing SQL commands into DDL, DML, DCL, and TCL. It explains various SQL data types, including numeric, character, date and time, binary, boolean, and special data types, along with examples of DDL commands like CREATE, ALTER, and DROP. Additionally, it covers DML commands for data manipulation, querying operations, and aggregate functions in SQL.

Uploaded by

wilsondougherty6
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)
5 views20 pages

DBMS-UnitIV - Final

This document provides an overview of Structured Query Language (SQL), detailing its purpose in managing relational databases and categorizing SQL commands into DDL, DML, DCL, and TCL. It explains various SQL data types, including numeric, character, date and time, binary, boolean, and special data types, along with examples of DDL commands like CREATE, ALTER, and DROP. Additionally, it covers DML commands for data manipulation, querying operations, and aggregate functions in SQL.

Uploaded by

wilsondougherty6
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/ 20

Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

UNIT IV - Structured Query Language

Structured Query Language(SQL):


SQL is a database language designed for the retrieval and management of data in a
relational database. SQL is the standard language for database management. All the
RDBMS systems like MySQL, MS Access, Oracle, Sybase, Postgres, and SQL Server use
SQL as their standard database language.
These SQL commands are divided into four types
1. DDL – Data Definition Language
2. DML – Data Manipulation Language
3. DCL – Data Control Language
4. TCL – Transaction control language

SQL Data types

SQL data types are used to define the type of data that can be stored in a column of a
database table. These data types ensure that data is stored in an efficient and consistent
manner. Below is a brief overview of common SQL data types:

1. Numeric Data Types

 INT / INTEGER: Stores whole numbers (e.g., -2147483648 to 2147483647).


 SMALLINT: Stores smaller range of whole numbers (e.g., -32768 to 32767).
 BIGINT: Stores very large whole numbers (e.g., -9223372036854775808 to
9223372036854775807).
 DECIMAL / NUMERIC: Stores fixed-point numbers with a specified precision and
scale (e.g., DECIMAL(10, 2) for numbers up to 10 digits long with 2 decimal
places).
 FLOAT: Stores approximate floating-point numbers.
 REAL: A smaller range of approximate floating-point numbers compared to
FLOAT.
 DOUBLE: Stores double-precision floating-point numbers.

1|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

2. Character Data Types

 CHAR(n): Fixed-length string, where n specifies the length (e.g., CHAR(10) stores
exactly 10 characters).
 VARCHAR2(n): Variable-length string, where n specifies the maximum length
(e.g., VARCHAR(255) stores up to 255 characters).
 TEXT: Stores large strings, typically for unstructured data like paragraphs of text.

3. Date and Time Data Types

 DATE: Stores date values (e.g., YYYY-MM-DD).


 TIME: Stores time values (e.g., HH:MM:SS).
 DATETIME: Stores both date and time (e.g., YYYY-MM-DD HH:MM:SS).
 TIMESTAMP: Similar to DATETIME, but often includes timezone information
and is used for recording the exact time of data entry.
 YEAR: Stores a year as a 2-digit or 4-digit value (e.g., YYYY).

4. Binary Data Types

 BINARY(n): Fixed-length binary data (e.g., BINARY(10) stores exactly 10


bytes).
 VARBINARY(n): Variable-length binary data, where n specifies the maximum
length (e.g., VARBINARY(255) stores up to 255 bytes).
 BLOB (Binary Large Object): Stores large binary data, typically for files like
images or multimedia.

5. Boolean Data Type

 BOOLEAN / BOOL: Stores TRUE or FALSE values (in some systems, represented
as 1 for TRUE and 0 for FALSE).

6. UUID

 UUID: Stores Universally Unique Identifier (UUID) values, which are 128-bit
numbers used to uniquely identify information in a distributed system.

7. Miscellaneous Data Types

 ENUM: Stores one value from a predefined list of values (e.g., ENUM('small',
'medium', 'large')).
 SET: Similar to ENUM, but allows storing multiple values from a predefined list.
 XML: Stores XML data, enabling structured document storage.
 ARRAY: Stores an array of values, supported in some SQL databases.

8. Special Data Types

 SERIAL: Often used for auto-incrementing primary keys, where each new row gets
a unique integer value.
 GEOMETRY: Stores geometric data (e.g., points, lines, polygons) for spatial
databases.
2|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

1. Data Definition Language (DDL)


DDL changes the structure of the table like creating a table, deleting a table, altering a table,
etc. All the command of DDL are auto-committed that means it permanently save all the
changes in the database.
Some DDL commands:
CREATE
ALTER
DROP
RENAME
CREATE :
There are two CREATE statements available in SQL:
1. CREATE DATABASE
2. CREATE TABLE

The CREATE DATABASE statement is used to create a new database in SQL.

Syntax: CREATE DATABASE database_name;

Where database_name: name of the database.

Example CREATE DATABASE NSCADB; -- NSCAdb is the database name


Create table: The CREATE TABLE statement is used to create a table in SQL. We know
that a table comprises of rows and columns. So, while creating tables we have to provide all
the information to SQL about the names of the columns, type of data to be stored in
columns, size of the data etc.
Syntax:
CREATE TABLE table_name
(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
....
);

Example:
CREATE TABLE Students
(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20),
);

3|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

ALTER (ADD, DROP, MODIFY, RENAME) :

ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is
also used to add and drop various constraints on the existing table.
ALTER TABLE – ADD
ADD is used to add columns into the existing table.
Syntax: ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,

Columnname_n datatype);

Example : alter table students add remarks varchar(20);

ALTER TABLE – DROP

DROP COLUMN is used to drop column in a table. Deleting the unwanted columns from
the table.
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;

Example: alter table students drop column subject;

ALTER TABLE-MODIFY

It is used to modify the existing columns in a table. Multiple columns can also be modified
at once.

Syntax : ALTER TABLE table_name MODIFY column_name column_type;

Example:
Alter table students modify remarks char(25);

ALTER TABLE-RENAME

The RENAME clause within the ALTER command specifically allows you to rename a
table or a column within a table.
1. Renaming a Table
The ALTER TABLE statement with the RENAME clause is used to rename an existing table to a
new name.
Syntax: ALTER TABLE old_table_name RENAME TO new_table_name;
Example: ALTER TABLE Employees RENAME TO Staff;
After executing this command, the table Employees will be renamed to Staff.

2. Renaming a Column
The ALTER TABLE statement can also be used to rename a column in a table.

4|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

Syntax: ALTER TABLE table_name RENAME COLUMN old_column_name TO


new_column_name;

Example : ALTER TABLE Employees RENAME COLUMN empName TO


employeeName;

After executing this command, the column empName will be renamed to employeeName in
the Employees table.

DROP

DROP is used to delete a whole database or just a table. The DROP statement destroys the
objects like an existing database, table, index, or view.
Syntax: DROP TABLE table_name;
table_name: Name of the table to be deleted.

Example : DROP TABLE students; -- students table is deleted

Syntax : DROP DATABASE database_name;


database_name: Name of the database to be deleted.
Example: drop database NSCA2DB; -- NSCA2db database name

RENAME
The RENAME command in SQL is used to change the name of an existing database object,
such as a table, view, or index. It is a straightforward and useful command that helps in
renaming objects without altering their structure or data.
1.RENAME a Table
The RENAME command is often used to rename a table.

Syntax : RENAME old_table_name TO new_table_name;

Example : RENAME old_table_name TO new_table_name;

2.RENAME a View
The RENAME command can also be used to rename a view.

Syntax: RENAME old_view_name TO new_view_name;

Example: RENAME View_Orders TO View_Purchases;

Data Manipulation Language


DML commands are used to modify the database. It is responsible for all form of changes in
the database. 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.
1. SELECT
2. INSERT
3. UPDATE
4. DELETE

5|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

SELECT
It is used to retrieve data from the database.
Syntax: SELECT column1, column2 FROM table_name
Example: SELECT ROLL_NO, NAME, AGE FROM Student;

INSERT INTO Statement

The INSERT INTO statement of SQL is used to insert a new row in a table. There are two
ways of using INSERT INTO statement for inserting rows:

1.Only values: First method is to specify only the value of data to be inserted without
the column names.
Syntax : INSERT INTO table_name VALUES (value1, value2, value3,…);
Example:
INSERT INTO Student VALUES (‘5′,’HARSH’,’WEST BENGAL’,’XXXXXXXXXX’,’19’);

2.Column names and values both: In the second method we will specify both the columns
which we want to fill and their corresponding values as shown below:
Syntax :
INSERT INTO table_name (column1, column2, column3,..) VALUES (value1, value2,
value3,….);
Example :
INSERT INTO Student (Roll_no, Name, Age) VALUES (‘5′,’PRATIK’,’19’);

UPDATE Statement

The UPDATE statement in SQL is used to update the data of an existing table in database.
We can update single columns as well as multiple columns using UPDATE statement as per
our requirement.
Syntax :
UPDATE table_name SET column1 = value1, column2 = value2,... WHERE
condition;
Example: UPDATE Student SET NAME = 'PRATIK' WHERE Age = 20;

DELETE Statement
The DELETE Statement in SQL is used to delete existing records from a table. We can
delete a single record or multiple records depending on the condition we specify in the
WHERE clause.
Syntax: Delete from tablename;

Syntax: DELETE FROM table_name WHERE some_condition;


Example: DELETE FROM Student WHERE NAME = 'Ram';

Querying (selection and projection) Operations:


Querying in MySQL involves retrieving data from one or more tables using the
SELECT statement, which can include various clauses and operations to filter, aggregate,
group, and order the data.

6|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

1. Basic SELECT with WHERE Clause


The SELECT statement is used to retrieve specific columns from a table. The WHERE clause is
used to filter rows based on a condition.
SELECT first_name, last_name, age FROM Employees WHERE age > 30;
This query selects the first_name, last_name, and age columns from the Employees table
where the age is greater than 30.

2. Arithmetic Operations
You can perform arithmetic operations directly within the SELECT statement.
QUERY : SELECT first_name, last_name, salary, salary * 0.1 AS bonus FROM
Employees WHERE age > 30;
This query selects the first_name, last_name, salary, and calculates a bonus as 10% of the
salary for employees older than 30.

3. Logical Operations (AND, OR, NOT)


Logical operators are used in the WHERE clause to combine multiple conditions.
QUERY : SELECT first_name, last_name, age, department FROM Employees WHERE
age > 30 AND department = 'Sales';
This query selects the first_name, last_name, age, and department of employees who are
older than 30 and work in the 'Sales' department.

4. Aggregation Functions
Aggregation functions such as COUNT, SUM, AVG, MIN, and MAX are used to perform
calculations on a set of values.
QUERY : SELECT department, AVG(salary) AS average_salary FROM Employees
WHERE age > 30
GROUP BY department;
This query calculates the average salary of employees older than 30, grouped by their
department.

5. Grouping (GROUP BY)


The GROUP BY clause groups rows that have the same values in specified columns into
summary rows, often used with aggregation functions.
QUERY: SELECT department, COUNT(*) AS number_of_employees FROM Employees
GROUP BY department;
This query counts the number of employees in each department.

6. Filtering Groups (HAVING Clause)


The HAVING clause is used to filter groups created by the GROUP BY clause.
QUERY:SELECT department, AVG(salary) AS average_salary FROM Employees
GROUP BY department HAVING AVG(salary) > 50000;
This query retrieves departments where the average salary is greater than 50,000.

7. Ordering (ORDER BY)


The ORDER BY clause is used to sort the result set by one or more columns.
QUERY:SELECT first_name, last_name, salary FROM Employees ORDER BY salary
DESC;
This query selects the first_name, last_name, and salary of employees and sorts the results
in descending order of salary.

7|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

MySQL Aggregate/Group Functions

Aggregate functions in Oracle SQL are used to perform calculations on a group of rows and
return a single value.

Sample Table: EMPLOYEES


EMP_ID NAME DEPARTMENT SALARY AGE
1 John HR 5000 35
2 Alice IT 7000 29
3 Bob IT 6000 32
4 Claire HR 4000 28
5 Eve Sales 6500 31
6 Frank Sales NULL 45

1. COUNT()
The COUNT() function counts the number of rows or non-NULL values in a column.
Query: Count the total number of employees.

SELECT COUNT(*) AS TOTAL_EMPLOYEES


FROM EMPLOYEES;

Output:
TOTAL_EMPLOYEES
6

Query: Count employees with non-NULL salaries.


SELECT COUNT(SALARY) AS EMPLOYEES_WITH_SALARY
FROM EMPLOYEES;
Output:
EMPLOYEES_WITH_SALARY
5

2. SUM()
The SUM() function calculates the total sum of a numeric column.
Query: Calculate the total salary paid to all employees.
SELECT SUM(SALARY) AS TOTAL_SALARY
FROM EMPLOYEES;

Output:
TOTAL_SALARY
28500

3. AVG()
The AVG() function calculates the average value of a numeric column.

8|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

Query: Find the average salary of employees.


SELECT AVG(SALARY) AS AVERAGE_SALARY
FROM EMPLOYEES;
Output:
AVERAGE_SALARY
5700

4. MAX()
The MAX() function returns the maximum value from a column.
Query: Find the highest salary among employees.
SELECT MAX(SALARY) AS HIGHEST_SALARY
FROM EMPLOYEES;
Output:
HIGHEST_SALARY
7000

5. MIN()
The MIN() function returns the minimum value from a column.
Query: Find the lowest salary among employees.
SELECT MIN(SALARY) AS LOWEST_SALARY
FROM EMPLOYEES;
Output:
LOWEST_SALARY
4000

Combining Functions with GROUP BY


Query: Find the total salary, average salary, maximum salary, and minimum salary for each
department.

SELECT
DEPARTMENT,
COUNT(*) AS EMPLOYEE_COUNT,
SUM(SALARY) AS TOTAL_SALARY,
AVG(SALARY) AS AVERAGE_SALARY,
MAX(SALARY) AS HIGHEST_SALARY,
MIN(SALARY) AS LOWEST_SALARY
FROM EMPLOYEES
GROUP BY DEPARTMENT;
Output:

DEPARTMENT EMPLOYEE_COUNT TOTAL_SALARY AVERAGE_SALARY HIGHEST_SALARY LOWEST_SALARY


HR 2 9000 4500 5000 4000
IT 2 13000 6500 7000 6000
Sales 2 6500 6500 6500 6500

9|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

Table Modification Commands

ALTER (ADD, DROP, MODIFY, RENAME) :

ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is
also used to add and drop various constraints on the existing table.
ALTER TABLE – ADD
ADD is used to add columns into the existing table.
Syntax: ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,

Columnname_n datatype);

Example : alter table students add remarks varchar(20);

ALTER TABLE – DROP

DROP COLUMN is used to drop column in a table. Deleting the unwanted columns from
the table.
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;

Example: alter table students drop column subject;

ALTER TABLE-MODIFY

It is used to modify the existing columns in a table. Multiple columns can also be modified
at once.

Syntax : ALTER TABLE table_name MODIFY column_name column_type;

Example:
Alter table students modify remarks char(25);

ALTER TABLE-RENAME

The RENAME clause within the ALTER command specifically allows you to rename a
table or a column within a table.
1. Renaming a Table
The ALTER TABLE statement with the RENAME clause is used to rename an existing table to a
new name.
Syntax: ALTER TABLE old_table_name RENAME TO new_table_name;
Example: ALTER TABLE Employees RENAME TO Staff;
After executing this command, the table Employees will be renamed to Staff.

2. Renaming a Column
The ALTER TABLE statement can also be used to rename a column in a table.
10 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

Syntax: ALTER TABLE table_name RENAME COLUMN old_column_name TO


new_column_name;

Example : ALTER TABLE Employees RENAME COLUMN empName TO


employeeName;

After executing this command, the column empName will be renamed to employeeName in
the Employees table.

SQL JOINS :
SQL joins are used to combine rows from two or more tables based on a related column
between them. Different types of joins determine how data from these tables is combined.

Example Tables:

Table Employees:
EmployeeID Name DepartmentID
1 Alice 10
2 Bob 20
3 Charlie 30
4 Dave NULL

Table Departments:
DepartmentID DepartmentName
10 HR
20 Finance
30 IT
40 Marketing

1. INNER JOIN
Definition: An INNER JOIN returns only the rows that have
matching values in both tables.

In the Venn diagram of an INNER JOIN, only the overlapping part


of the circles (representing the rows that match in both tables) is
returned.

Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

Example :

SELECT Employees.Name, Departments.DepartmentName


FROM Employees
INNER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
11 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

Output:
Name DepartmentName
Alice HR
Bob Finance
Charlie IT

2. LEFT JOIN (or LEFT OUTER JOIN)


Definition: A LEFT JOIN returns all the rows from the left table and the matching rows
from the right table. If there is no match, the result is NULL on the side of the right table.

In the Venn diagram of a LEFT JOIN, the entire left circle is


included, along with the intersection part, representing all rows
from the left table, with matching rows from the right table (and
NULL where there is no match).

Syntax:

SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
Example :
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;

Output:

Name DepartmentName
Alice HR
Bob Finance
Charlie IT
Dave NULL

3. RIGHT JOIN (or RIGHT OUTER JOIN)


Definition: A RIGHT JOIN returns all the rows from the right table and the matching rows
from the left table. If there is no match, the result is NULL on the
side of the left table.

In the Venn diagram of a RIGHT JOIN, the entire right circle is


included, along with the intersection part, representing all rows
from the right table, with matching rows from the left table (and
NULL where there is no match).

Syntax:
SELECT columns
FROM table1
12 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

RIGHT JOIN table2


ON table1.column = table2.column;

Example Query:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Output:

Name DepartmentName
Alice HR
Bob Finance
Charlie IT
NULL Marketing

4. FULL JOIN (or FULL OUTER JOIN)

Definition: A FULL JOIN returns all rows when there is a match in either the left or right
table. If there is no match, the result is NULL from the
non-matching side.

In the Venn diagram of a FULL JOIN, both circles


and their intersection are included, representing all rows
from both tables, with NULL in places where there is no
match.

Syntax:
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
Example Query:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
FULL JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Output:
Name DepartmentName
Alice HR
Bob Finance
Charlie IT
Dave NULL
NULL Marketing

5. CROSS JOIN
Definition: A CROSS JOIN returns the Cartesian product of the two tables, meaning it
combines all rows from the left table with all rows from the right table.

13 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

Syntax:
SELECT columns
FROM table1
CROSS JOIN table2;
Example Query:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
CROSS JOIN Departments;
Output:

Name DepartmentName
Alice HR
Alice Finance
Alice IT
Alice Marketing
Bob HR
Bob Finance
Bob IT
Bob Marketing
Charlie HR
Charlie Finance
Charlie IT
Charlie Marketing
Dave HR
Dave Finance
Dave IT
Dave Marketing

6. SELF JOIN
Definition: A SELF JOIN is a join of a table with itself. This is useful when the table has a
hierarchical relationship.
Syntax:
SELECT A.column, B.column
FROM table A
INNER JOIN table B
ON A.column = B.column;
Example Query:
SELECT A.Name AS Employee, B.Name AS Manager
FROM Employees A
INNER JOIN Employees B
ON A.ManagerID = B.EmployeeID;

Output: This would list employees alongside their managers, assuming the Employees table
has a ManagerID column referencing the EmployeeID.

A SELF JOIN is conceptually similar to an INNER JOIN, but it's between two copies of the
same table. There's no specific Venn diagram since it's the same table joined with itself.

14 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

SQL Set Operation

The SQL Set operation is used to combine the two or more SQL SELECT statements.
Types of Set Operations
1. Union
2. UnionAll
3. Intersect
4. Minus

1. Union
 The SQL Union operation is used to combine the result of two or more SQL
SELECT queries.
 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.
 The union operation eliminates the duplicate rows from its result set.

Syntax SELECT column_name FROM table1


UNION
SELECT column_name FROM table2;
Example:
The First tableM89 The Second table JRE, and JVM

ID NAME
ID NAME

3
1 Jack

2 Harry
4 Stephan
3 Jackson
5 David
Union SQL query will be:

SELECT * FROM First


UNION
SELECT * FROM Second;
15 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

The result set table will look like:

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;

Example: Using the above First and Second table.

Union All query will be like:


SELECT * FROM First
UNION ALL
SELECT * FROM Second;

The result set table will look like:


ID NAME

1 Jack

2 Harry

3 Jackson

3 Jackson

4 Stephan

5 David

16 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

3. Intersect
 It is used to combine two SELECT statements. The Intersect operation returns the
common rows from both the SELECT statements.
 In the Intersect operation, the number of datatype and columns must be the same.
 It has no duplicates and it arranges the data in ascending order by default.

Syntax
SELECT column_name FROM table1
INTERSECT
SELECT column_name FROM table2;
Example:
Using the above First and Second table.
Intersect query will be:
SELECT * FROM First
INTERSECT
SELECT * FROM Second;

The resultset table will look like:


ID NAME

3 Jackson

4. Minus
 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.
 It has no duplicates and data arranged in ascending order by default.

Syntax:
SELECT column_name FROM table1
MINUS
SELECT column_name FROM table2;

Example
Using the above First and Second table.
Minus query will be:

SELECT * FROM First


MINUS
SELECT * FROM Second;

17 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

The resultset table will look like:

ID NAME

1 Jack

2 Harry

SQL 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.

A view is created with the CREATE VIEW statement.

CREATE VIEW Syntax


CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Note: A view always shows up-to-date data! The database engine recreates the view, every
time a user queries it.

SQL CREATE VIEW Examples

The following SQL creates a view that shows all customers from Brazil:

CREATE VIEW [Brazil Customers] AS


SELECT CustomerName, ContactName
FROM Customers
WHERE Country = 'Brazil';

SQL Sub Query

A Subquery is a query within another SQL query and embedded within the WHERE clause.

18 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

Important Rule:

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.

Subqueries with the Select Statement

SQL subqueries are most frequently used with the Select statement.
Syntax
SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT column_name from table_name WHERE ... );

Example

Consider the EMPLOYEE table have the following records:

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

6 Harry 42 China 4500.00

7 Jackson 25 Mizoram 10000.00

19 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST

The subquery with a SELECT statement will be:

SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE
WHERE SALARY > 4500);

This would produce the following result:

ID NAME AGE ADDRESS SALARY

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

7 Jackson 25 Mizoram 10000.00

*******

20 | P a g e

You might also like