Sie sind auf Seite 1von 29

DBMS - LEARNER'S NOTES

Introduction to Database:
1.0 Introduction :
Database is a collection of related data. Database management system is software
designed to assist the maintenance and utilization of large scale collection of data. DBMS
came into existence in 1960 by Charles. Integrated data store which is also called as the
first general purpose DBMS. Again in 1960 IBM brought IMS-Information management
system. In 1970 Edgor Codd at IBM came with new database called RDBMS. In 1980
then came SQL Architecture- Structure Query Language. In 1980 to 1990 there were
advances in DBMS e.g. DB2, ORACLE.

Data
• Data is raw fact or figures or entity.
• When activities in the organization takes place, the effect of these activities need
to be recorded which is known as Data.

Information
• Processed data is called information
• The purpose of data processing is to generate the information required for
carrying out the business activities.
In general data management consists of following tasks
• Data capture: Which is the task associated with gathering the data as and when
they originate.
• Data classification: Captured data has to be classified based on the nature and intended
usage.
• Data storage: The segregated data has to be stored properly.
• Data arranging: It is very important to arrange the data properly
• Data retrieval: Data will be required frequently for further processing,
Hence it is very important to create some indexes so that data can be retrieved
easily.
• Data maintenance: Maintenance is the task concerned with keeping the data upto-
date.
• Data Verification: Before storing the data it must be verified for any error.
• Data Coding: Data will be coded for easy reference.
• Data Editing: Editing means re-arranging the data or modifying the data for presentation.
• Data transcription: This is the activity where the data is converted from one form into
another.
• Data transmission: This is a function where data is forwarded to the place where it would
be used further.

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Metadata (meta data, or sometimes meta information) is "data about data", of any sort
in any media. An item of metadata may describe a collection of data including multiple
content items and hierarchical levels, for example a database schema. In data processing,
metadata is definitional data that provides information about or documentation of other
data managed within an application or environment. The term should be used with
caution as all data is about something, and is therefore metadata.

Database
• Database may be defined in simple terms as a collection of data
• A database is a collection of related data.
• The database can be of any size and of varying complexity.
• A database may be generated and maintained manually or it may be computerized.

Database Management System


• A Database Management System (DBMS) is a collection of program that enables
user to create and maintain a database.
• The DBMS is hence a general purpose software system that facilitates the process
of defining constructing and manipulating database for various applications.

1.1 Characteristics of DBMS


• To incorporate the requirements of the organization, system should be designed
for easy maintenance.
• Information systems should allow interactive access to data to obtain new
information without writing fresh programs.
• System should be designed to co-relate different data to meet new requirements.
• An independent central repository, which gives information and meaning of
available data is required.
• Integrated database will help in understanding the inter-relationships between data
stored in different applications.
• The stored data should be made available for access by different users
simultaneously.
• Automatic recovery feature has to be provided to overcome the problems with
processing system failure.

DBMS Utilities
• A data loading utility:
Which allows easy loading of data from the external format without writing
programs.
• A backup utility:
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Which allows to make copies of the database periodically to help in cases of


crashes and disasters.
• Recovery utility:
Which allows to reconstruct the correct state of database from the backup and
history of transactions.
• Monitoring tools:
Which monitors the performance so that internal schema can be changed and
database access can be optimized.
• File organization:
Which allows restructuring the data from one type to another?

1.2 Difference between File system & DBMS


File System
1. File system is a collection of data. Any management with the file system, user has to
write the procedures
2. File system gives the details of the data representation and Storage of data.
3. In File system storing and retrieving of data cannot be done efficiently.
4. Concurrent access to the data in the file system has many problems like
a. Reading the file while other deleting some information, updating some information
5. File system doesn’t provide crash recovery mechanism.
Eg. While we are entering some data into the file if System crashes then content of the
file is lost.
6. Protecting a file under file system is very difficult.

DBMS
1. DBMS is a collection of data and user is not required to write the procedures for
managing the database.
2. DBMS provides an abstract view of data that hides the details.
3. DBMS is efficient to use since there are wide varieties of sophisticated techniques to
store and retrieve the data.
4. DBMS takes care of Concurrent access using some form of locking.
5. DBMS has crash recovery mechanism, DBMS protects user from the effects of system
failures.
6. DBMS has a good protection mechanism.

DBMS = Database Management System


RDBMS = Relational Database Management System

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

A database management system is, well, a system used to manage databases.


A relational database management system is a database management system used to
manage relational databases. A relational database is one where tables of data can have
relationships based on primary and foreign keys.

1.3 Advantages of DBMS.


Due to its centralized nature, the database system can overcome the disadvantages
of the file system-based system
1. Data independency:
Application program should not be exposed to details of data representation and storage
DBMS provides the abstract view that hides these details.
2. Efficient data access.:
DBMS utilizes a variety of sophisticated techniques to store and retrieve data
efficiently.
3. Data integrity and security:
Data is accessed through DBMS, it can enforce integrity constraints.
E.g.: Inserting salary information for an employee.
4. Data Administration:
When users share data, centralizing the data is an important task, Experience
professionals can minimize data redundancy and perform fine tuning which reduces
retrieval time.
5. Concurrent access and Crash recovery:
DBMS schedules concurrent access to the data. DBMS protects user from the effects of
system failure.
6. Reduced application development time.
DBMS supports important functions that are common to many applications.

1.4 Functions of DBMS:


• Data Definition: The DBMS provides functions to define the structure of the data
in the application. These include defining and modifying the record structure, the
type and size of fields and the various constraints to be satisfied by the data in
each field.
• Data Manipulation: Once the data structure is defined, data needs to be inserted,
modified or deleted. These functions which perform these operations are part of
DBMS. These functions can handle plashud and unplashud data manipulation
needs. Plashud queries are those which form part of the application. Unplashud
queries are ad-hoc queries which performed on a need basis.
• Data Security & Integrity: The DBMS contains modules which handle the
security and integrity of data in the application.
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

• Data Recovery and Concurrency: Recovery of the data after system failure and
concurrent access of records by multiple users is also handled by DBMS.
• Data Dictionary Maintenance: Maintaining the data dictionary which contains the
data definition of the application is also one of the functions of DBMS.
• Performance: Optimizing the performance of the queries is one of the important
functions of DBMS.

1.5 Role of Database Administrator.


Typically there are three types of users for a DBMS:

1. The END User who uses the application. Ultimately he is the one who actually
puts the data into the system into use in business. This user need not know
anything about the organization of data in the physical level.

2. The Application Programmer who develops the application programs. He/She has
more knowledge about the data and its structure. He/she can manipulate the data
using his/her programs. He/she also need not have access and knowledge of the
complete data in the system.

3. The Data base Administrator (DBA) who is like the super-user of the system.
The role of DBA is very important and is defined by the following functions.
• Defining the schema: The DBA defines the schema which contains the
structure of the data in the application. The DBA determines what data
needs to be present in the system and how this data has to be presented
and organized.

• Liaising with users: The DBA needs to interact continuously with the
users to understand the data in the system and its use.
• Defining Security & Integrity checks: The DBA finds about the access
restrictions to be defined and defines security checks accordingly. Data
Integrity checks are defined by the DBA.
• Defining Backup/Recovery Procedures: The DBA also defines procedures
for backup and recovery. Defining backup procedure includes specifying
what data is to be backed up, the periodicity of taking backups and also
the medium and storage place to backup data.
• Monitoring performance: The DBA has to continuously monitor the
performance of the queries and take the measures to optimize all the
queries in the application.

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

ARITHMETIC, LOGICAL AND COMPARISON OPERATORS

Aim
To execute a set of SQL statements using arithmetic, logical and comparison operators.
TABLE EMPLOYEES:
create table employees(eid varchar(4),ename varchar(5),edept varchar(15),esalary
number(6));
select * from employees;

1. ARITHMETIC OPERATORS ( +, -, *, / )
Query: 1. select eid, ename, esalary*2 from employees;
2. update employees set esalary=esalary*2;
select * from employees;

2. LOGICAL OPERATORS
1.AND:
Write a query to display the details of employee whose salary is between 20000 and 50000.
Syntax: select (column_name 1,...,column_name n) from <table_name> where
search_condition1 and
search_condition2;
Query: select * from employees where esalary>20000 and esalary<50000;

2. OR:
Write a query to display the details of employee whose id is A101 or B102.
Syntax:
select (column_name 1,...,column_name n)
from <table_name> where search_condition1 or
search_condition2;
Query: select * from employees where eid=‘A101’ or eid=‘B102’;

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

3. NOT
Write a query to display the details of employee whose id is not A102.
Syntax: select (column_name 1,...,column_name n) from <table_name> where not
search_condition;
Query: select * from employees where not eid=‘A102’;

3. COMPARISON OPERATORS:

1.NOT BETWEEN:
Write a query to display the names of employee whose salary is not between 20000 and
60000.
Syntax: select (column_name 1,...,column_name n) from <table_name> where column_name
not
between value1 and value2;
Query: select ename from employees where esalary not between 20000 and 60000;

2.BETWEEN:
Write a query to display the names of employee whose salary is between 30000 and 60000.
Syntax: select (column_name 1,...,column_name n) from <table_name> where column_name
between value1 and value2;
Query: select ename from employees where esalary between 30000 and 60000;

3. LIKE ( % - string or _ - character )


Syntax:
column_name starting with <character>:
select (column_name 1,...,column_name n) from <table_name>
where column_name like
‘<character>%’;column_name ending with <character>:
select (column_name 1,...,column_name n) from <table_name>
where column_name like‘%<character>‘;

a.Write queries to display the details of employee whose name begins with character ‘a’, ends
with character ‘z’.
Queries: select * from employees where ename like ‘a%’;
select * from employees where ename like ‘%z’;

b. Write a query to display the names of employee whose name has a length of four
characters
Query: select ename from employees where ename like ‘____’;
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

4. NOT LIKE:
Write a query to display the details of employee whose name does not begin with character
‘a’
Query: select * from employees where ename not like ‘a%’;

5. IN:
Write a query to display the details of employee whose name is abc , xyz or def.
Syntax: select (column_name 1,...,column_name n) from <table_name> where column_name
in
(<value1>,<value 2>,...,<value n>);
Query: select * from employees where ename in ( ‘abc’,’xyz’,’def’);

6. NOT IN:
Write a query to display the details of employee whose name is not abc , xyz or def.
Syntax: select (column_name 1,...,column_name n) from <table_name> where column_name
not in(<value1>,<value 2>,...,<value n>);
Query: select * from employees where ename not in ( ‘abc’,’xyz’,’def’);

4. MISCELLANEOUS FUNCTIONS

1. ORDER BY : Orders the resultant rows either in ascending or descending order.


Write queries to display the details of employees ordered in ascending and descending by
their names.
Syntax: select (column_name 1,...,column_name n) from <table_name> order by
column_name asc;
select (column_name 1,...,column_name n) from <table_name> order by column_name desc;
Query: select * from employees order by ename asc;
select * from employees order by ename desc;

2. GROUP BY : Groups the rows based on distinct values that exist for specified column.
Write a query to display the individual total salary amount of all the departments.
Syntax: select column_name,aggregate_function(column_name) from <table_name> group
by column_name;
Query: select edept,sum(esalary) from employees group by edept;

3. HAVING : Output the rows of the query having the specified values.
Write a query to display the individual total salary amount of departments having the total
amount greater than100000.

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Syntax: select column_name,aggregate_function(column_name) from <table_name> group


by column_name having aggregate_function(column_name) operator value;
Query: select edept,sum(esalary) from employees group by edept having
sum(esalary)>100000;

4. AS : Changes the column_name during display of table data.


Write a query to display the names of employees as EMPLOYEE_LIST
Syntax: select <column_name1> as <new name> from <table_name>;
Query: select ename as EMPLOYEE_LIST from employees;
Result:Thus a set of SQL statements using arithmetic, logical and comparison operators were
implemented successfully.

AGGREGATE, CHARACTER, NUMERICAL AND DATE FUNCTIONS


Aim:
To execute a set of SQL statements using aggregate, character and numerical functions.

TABLE DUAL:
•The dual table is a temporary table which contains exactly one column whose name is
dummy and a single row. The value of that record is X. The owner of dual is SYS (SYS is the
owner of the database and the owner of the data dictionary) but dual can be accessed by every
user.

select * from dual;


TABLE EMPLOYEES:
•create table employees(emp_id
varchar(4),emp_name varchar(5),emp_dept
varchar(15),salary number(6));
select * from employees;

I AGGREGATE FUNCTIONS

1. COUNT:
It is used to count the no of rows or records in a table.
a.
Syntax: select count(*) from <table_name> ;
Ex: select count(*) from employees;
b. Syntax: select count(<column_name>) from <table_name> ;
Ex: select count(emp_name) from employees;
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

2. SUM
It is used to compute the sum of a particular field in a table.
Syntax: select sum(<column_name>) from <table_name> ;
Ex: select sum(salary) from employees;

3. AVERAGE
It is used to compute the average of a particular field in a table.
Syntax: select avg(<column_name>) from <table_name> ;
Ex: select avg(salary) from employees;

4. MINIMUM
It is used to minimum value of a particular field in a table.
Syntax: select min(<column_name>) from <table_name> ;
Ex: select min(salary) from employees;

5. MAXIMUM
It is used to maximun value of a particular field in a table.
Syntax: select max(<column_name>) from <table_name> ;
Ex: select max(salary) from employees;

II CHARACTER FUNCTIONS
1. UPPER
Returns the given string with all the characters in uppercase.
Syntax: select upper(<string>) from <table_name> ;
Ex: select upper(emp_name) from employees;
select upper(‘hello’) from dual;

2. LOWER
Returns the given string with all the characters in lowercase.
Syntax: select lower(<string>) from <table_name> ;
Ex: select lower(emp_name) from employees;
select lower(‘hello’) from dual;

3. SUBSTR
Returns the substring selected from the given string. The substring starts from nth
character of the given string and m is the length of the substring.

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Syntax: select substr(<string>,<n – start of substr>,<m – length of substr>) from


<table_name> ;
Ex: select substr(emp_dept,1,3) from employees;
select substr(‘abcdef’,3,2) from dual;

4. CONCAT
Concatenates the two given strings.
Syntax: select concat(<string1>,<string2>) from <table_name> ;
Ex: select concat(emp_id,emp_dept) from employees;
select concat(‘hai’,’hello’) from dual;

5. TRANSLATE
Returns the given string after replacing all the occurrence of each character in str1 with the
characters from str2.
Syntax: select translate(<string>,<str1>,<str2>) from <table_name> ;
Ex: select translate(‘rack’,’r’,’j’) from dual;

6. ASCII
Returns the decimal equivalent of a single ASCII character.
Syntax: select ascii(<character>) from <table_name> ;
Ex: select ascii(‘b’) from dual;

7. CHR
Returns the ASCII character represented by the given decimal number.
Syntax: select chr(<n>) from <table_name> ;
Ex: select chr(‘97’) from dual;

8. LENGTH
Returns the length of the given string.
Syntax: select length(<string>) from <table_name> ;
Ex: select length(‘database’) from dual;

9. LPAD
Performs left padding of the given string up to length specified the user with the specified
character.
Syntax: select lpad(<string>,<n-length of the resultant string>,<character>) from
<table_name> ;
Ex: select lpad(‘database’,10,’*’) from dual;

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

10. RPAD
Performs right padding of the given string up to length specified the user with the specified
character.
Syntax: select rpad(<string>,<n-length of the resultant string>,<character>) from
<table_name> ;
Ex: select rpad(‘database’,10,’*’) from dual;

11. INITCAP
Capitalizes starting letter of each word in a given string.
Syntax: select initcap(<string>) from <table_name> ;
Ex: select initcap(emp_name) from employees;
select initcap(‘database’) from dual;

III NUMERICAL FUNCTIONS:


1. ABS
Returns the absolute value of the given number.
Syntax: select abs(<n>) from <table_name> ;
Ex: select abs(-23) from dual;

2. CEIL
Returns the greatest integer value equal to or greater than the given number.
Syntax: select ceil(<n>) from <table_name> ;
Ex: select ceil(23.4) from dual;

3. FLOOR
Returns the smallest integer value equal to or smaller than the given number.
Syntax: select floor(<n>) from <table_name> ;
Ex: select floor(23.4), floor(34.7), floor(39.2) from dual ;

4. MOD
Returns the value of ( n modulus r).
Syntax: select mod(<remainder>,<n>) from <table_name> ;
Ex: select mod(20,3) from dual;

5. POWER
Returns the value of (mn ).
Syntax: select power(<m>,<n>) from <table_name> ;
Ex: select power(20,3) from dual;

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

6. ROUND
Returns the value of n rounded to m places right of decimal point. If value of m is not
mentioned, n is rounded to 0 places.
Syntax: select round(<n>,<m>) from <table_name> ;
Ex: select round(35.3333,1) from dual;

7. SIGN
Used to check the sign of a number. [ -1 if n<0, 0 if n=0, 1 if n>0 ]
Syntax: select sign(<n>) from <table_name> ;
Ex: select sign(3) from dual;

8. SQRT
Returns the squareroot of the given number.
Syntax: select sqrt(<n>) from <table_name> ;
Ex: select sqrt(9) from dual;

9. TRUNC
Value of n will be truncated to m decimal places. If value of m is not mentioned, n will be
truncated to 0 places.
Syntax: select trunc(<n>,<m>) from <table_name> ;
Ex: select trunc(333.123,1) from dual;

10. COS
Returns the cosine value of any number passed as an angle in radians to the function.
Syntax: select cos(<n>) from <table_name> ;
Ex: select cos(0) from dual;

11. SIN
Returns the sine value of any number passed as an angle in radians to the function.
Syntax: select sin(<n>) from <table_name> ;
Ex: select sin(0) from dual;

12. TAN
Returns the tangent value of any number passed as an angle in radians to the function.
Syntax: select tan(<n>) from <table_name> ;
Ex: select tan(0) from dual;

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

13. EXP
Returns the value of (e ) , where e=2.71828183...
Syntax: select exp(<n>) from <table_name> ;
Ex: select exp(0) from dual;

14. LOG
nReturns logarithm value of n with base b.
Syntax: select log(<b>,<n>) from <table_name> ;
Ex: select log(2,2) from dual;

15. LN
Returns natural logarithm value of n, where n>0.
Syntax: select ln(<n>) from <table_name> ;
Ex: select ln(1) from dual;

16. LEAST
Returns the least value among the given numbers.
Syntax: select least(<n1>,<n2>,<n3>,...) from <table_name> ;
Ex: select least(56,99,34,1,23) from dual;

17. GREATEST
Returns the greatest value among the given numbers.
Syntax: select greatest(<n1>,<n2>,<n3>,...) from <table_name> ;
Ex: select greatest(56,99,34,1,23) from dual;

III DATE FUNCTIONS:

1. SYSDATE
Returns the current date and time in the Oracle server.
Syntax: select sysdate from <table_name> ;
Ex: select sysdate from dual;

2. ADD_MONTHS
Adds or subtracts months from a date.
Syntax: select add_months(<date>,<no of months>) from <table_name> ;
Ex: select add_months(’20-jan-13’,2) from dual;

3. LAST_DAY
Provides the last day of the month specified in the given date.
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Syntax: select last_day(<date>) from <table_name> ;


Ex: select last_day(sysdate) from dual;

4. TO_CHAR
Converts date into charater format.
Syntax:
select to_char(<date>,’mm’) from <table_name> ;
select to_char(<date>,’dd’) from <table_name> ;
select to_char(<date>,’yyyy’) from <table_name> ;
Ex: select to_char(sysdate,’mm’) from dual ;
select to_char(sysdate,’dd’) from dual ;
select to_char(sysdate,’yyyy’) from dual ;

5. TO_DATE
Converts string into date format
Syntax: select to_date('<string>’, 'Month dd, YYYY’) from <table_name> ;
Ex: select to_date('January 26 2013', 'Month dd, YYYY’) from dual;
Result
Thus a set of SQL statements using aggregate, character, numerical and date functions were
implemented successfully.

IMPLEMENTATION OF DML, DQL AND TCL

To execute a set of SQL statements implementing DML, DQL and TCL commands
DML – Data Manipulation Language:
The Data Manipulation Language (DML) is used to access or manipulate the data organized
by an appropriate data model.
DQL – Data Query Language:
The Data Query Language (DQL) enables the users to retrieve information from the
database.
It is used to display the data stored in a table.
TCL – Transaction Control Language:
The Transaction Control language (TCL) enables the users to control the changes made
by atransaction in a database.

2.1 DML COMMANDS:

1. INSERT: It is used to insert rows into an existing table.


a) Write a query to insert the record(1001,’abcd’,’programmer’,25000) into employee table.
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Syntax: SQL> insert into <table name> values(val1, val2,…….. val n.);
Query: SQL> insert into employee values(1001,’abcd’,’programmer’,25000);
b) Write a query to insert the record (1002,’def’,’analyst’,30000) into employee table using
subtitution method
Syntax: SQL > insert into <table name> values(& column name1, &column name2,…..);
Query: SQL> insert into employee values(&empno,&ename,&desgn,&salary);

2. UPDATE: It is used to update or change the values of a particular column of a table.


a) Write a query to update the salary of employee ‘def’ to 15000 in employee table.
Syntax: SQL> update <table name> set <column name > = <new value> where
<condition>;
Query: SQL> update employee set salary=15000 where ename=’def’;

3. DELETE: It is used to delete particular rows from a table.


a) Write a query to delete the record of employee ‘abcd’ from employee table.
Syntax: SQL> delete from <table name> where <condition>;
Query: SQL> delete from employee where ename = ‘abcd’;

4. TRUNCATE : It is used to delete all the rows from the table and free the space containing
the table.
a) Write queries to insert and delete the contents of table emp1.
Syntax: SQL>truncate table <table name>;
Query: SQL>insert into emp1 values( 1, ‘abc’);

SQL> select * from emp1;


SQL>truncate table emp1;
SQL> select * from emp1;

2.2 DQL COMMAND:

1. SELECT: It is used to display rows from table.


a) Write a query to display all the rows from employee table.
Syntax: SQL > select * from <table name>;
Query: SQL> select * from employee;
b) Write a query to display the rows from employee table where salary>20000.
Syntax: SQL > select * from <table name> where <condition>;
Query: SQL> select * from employee where salary>20000;
c) Write a query to display the empno and names of all the employees.

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Syntax: SQL > select <column name1,column name2...column name n> from <table name>
;
Query: SQL> select empno,ename from employee;
d) Write a query to display the names of the employees who are programmers.
Syntax: SQL > select <column name1,column name2...column name n> from <table name>
where <condition>;
Query: SQL> select empno,ename from employee where desgn =’programmer’;
e) Write a query to display the different designations available.
Syntax: SQL > select distinct column name from <table name> ;
Query: SQL> select distinct desgn from employee;

2.3 TCL COMMANDS

1. SAVEPOINT: It is used to save the changes made until the particular timepoint.
Syntax: SQL > savepoint <savepoint name> ;
Query: SQL>savepoint svp;

2. ROLLBACK: It is used to undo the changes done by the current transaction until a
particular
timepoint.
Syntax: SQL > rollback <savepoint name>;
Query: SQL>rollback svp;

3. COMMIT: It is used to end the current transaction and to save the changes made by the
transaction.
Syntax: SQL > commit ;
Query: SQL>commit;
RESULT
Thus a set of SQL statements implementing DML, DQL and TCL commands were
executed
successfully.

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

IMPLEMENTATION OF DDL :
AIM
To execute a set of SQL statements implementing DDL commands and Constraints

DDL – Data Definition Language:


The Data Definition Language (DDL) is used to create and destroy databases and
database objects. It is typically considered to be a subset of SQL, the Structured Query
Language. These commands will primarily be used by database administrators during the
setup and removal phases of a database project.

1.1 DDL COMMANDS


1.CREATE : It is used to create objects in the database.
a) Write a query to create a table employee with empno, ename, designation, and salary.
Syntax: SQL> create table <tablename> (column name.1 <datatype> (size), column name.2
<datatype> (size)……… );
Query: SQL>create table employee ( empno number(4), ename char(10), designation
varchar(10), salary number(5));
b) Write a query to create a new table emp1 from an existing table emp with selected fields.

Syntax : SQL> create table <target table name> as select (column name.1, column name.2
,…………column name n ) from <soursce table name>;
Query: SQL> create table emp1 as select ( empno, ename) from employee;

2.DESC : It is used to display the description of the database objects.


a) Write a query to display the column name and data type of the table employee.
Syntax: SQL> desc <table name>;
Query: SQL>desc employee ;

3.ALTER : It is used to alter the structure of database.


a) Write a query to alter the table employee with column empno number(4) to empno
number(6).
Syntax: SQL>alter table <table name> modify (columnname datatype (size));
Query: SQL> alter table employee modify (empno number(6));
b) Write a query to alter multiple columns (empno,ename) of the table employee.
Syntax: SQL>alter table <table name> modify (columnname1 datatype (size),
columnname2 datatype (size), ……………. Columnname n datatype (size));
Query: SQL> alter table employee modify (empno number(8), ename char(15));
c) Write a query to add a new column empdept into the table employee.
Syntax: SQL>alter table <table name> add (columnname datatype (size));
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Query: SQL>alter table employee add (empdept char (4));


d) Write a Query to add multiple columns into the table employee.
Syntax: SQL>alter table <table name> add (columnname1 datatype (size), columnname2
datatype (size), …………… columnname n datatype (size));
Query: SQL>alter table employee add (empcity char (4), empphone number(15));

4.RENAME : It is used to rename the objects.


a) Write a Query to rename the old table name into new table name
Syntax: SQL>alter table <table name> rename <old table name> to <new table name>;
Query: SQL>alter table employee rename employee to emp;

5.TRUNCATE : It is used to delete all the rows from the table and free the space containing
the table.
a) Write queries to insert and delete the contents of table emp1.
Syntax: SQL>truncate table <table name>;
Query: SQL>insert into emp1 values( 1, ‘abc’);
SQL> select * from emp1;
SQL>truncate table emp1;
SQL> select * from emp1;

6.DROP: It is used to delete the object from the database.


a) Write a query to drop a column from an existing table
Syntax: SQL>alter table <table name> drop column <column name>;
Query: SQL> alter table emp drop column empphone;
b) Write a Query to drop multiple columns from an existing table
Syntax: SQL>alter table <table name> drop column(column name1, column name1,
……. column name n);
Query: SQL> alter table emp drop column(empdept,empcity);
c) Write a query to delete an existing table.
Syntax: SQL>drop table <tablename>;
Query: SQL>drop table emp;

1.2 CONSTRAINTS
Constraints are part of the table definition that limits and restricts the value entered into
its column.
Constraints can be created in three ways: Operations on Constraints:
 Column level constraints  Enable
 Table level constraints  Disable
 Using DDL command - alter  Drop
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

TYPES OF CONSTRAINTS:

1. NOT NULL : This constraint enforces a column to not accept NULL values.The NOT
NULL constraint enforces a field to always contain a value.
Column level constraint: Write a query to create not null constraint at column level.
Syntax: SQL> create table <table.name> (column name1 <datatype> (size) <constraint
type>, columnname.2<datatype> (size));
Query: SQL> create table student (rollno number(5) NOT NULL , dept char(5), name
char(15));

2. UNIQUE : This constraint uniquely identifies each record in a database table.The


UNIQUE and PRIMARY
KEY constraints both provide a guarantee for uniqueness for a column or set of columns.
a)Column level constraint:
Write a query to create unique constraint at column level.
Syntax: SQL> create table <table.name> (column name1 <datatype> (size) UNIQUE,
columnname.2<datatype> (size));
Query: SQL> create table student (rollno number(5) UNIQUE , dept char(5), name
char(15));
a) Column level constraint with naming convention:
Write a query to create unique constraint at column level.
Syntax: SQL> create table <table.name> (column name1 <datatype> (size) constraint
<constraint_name> UNIQUE,columnname.2<datatype> (size));
Query: SQL> create table student (rollno number(5) constraint consrt UNIQUE , dept
char(5), name char(15));

b) Table level constraint:


Write a query to create unique constraint at table level.
Syntax: SQL> create table <table.name> (column name1 <datatype> (size),
columnname.2<datatype> (size),…. ,constraint <constraint_name> <constraint type>
(columnname1, columnname2,…));
Query: SQL> create table student (rollno number(5) NOT NULL , dept char(5) NOT
NULL, name char(15),constraint stud unique( rollno, dept));

c) Specifying constraint with alter command:


Write queries to create unique constraint at table level using alter command.
Syntax: SQL> create table <tablename> (column name.1 <datatype> (size), column
name.2 <datatype> (size)…… );
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

SQL>alter table <table name> add constraint <constraint name> UNIQUE (column name)
Query: SQL> create table student (rollno number(5), dept char(5), name char(15));
SQL> alter table student add constraint consrt UNIQUE(rollno);

3. PRIMARY KEY: This constraint uniquely identifies each record in a database


table.Primary keys must contain unique values.A primary key column cannot contain
NULL values. A PRIMARY KEY constraint automatically has a UNIQUE constraint
defined on it. There may be many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.

a) Column level constraint:


Write a query to create primary key constraint at column level.
Syntax: SQL> create table <table.name> (column name1 <datatype> (size) PRIMARY
KEY, columnname.2<datatype> (size));
Query: SQL> create table student (rollno number(5) PRIMARY KEY , dept char(5),
name char(15));

b) Column level constraint with naming convention:


Write a query to create primary key constraint at column level.
Syntax: SQL> create table <table.name> (column name1 <datatype> (size) constraint
<constraint_name> PRIMARY KEY, columnname.2<datatype> (size)); Query:
SQL> create table student (rollno number(5) constraint consrt PRIMARY KEY , dept
char(5), name char(15));

c) Table level constraint:


Write a query to create primary key constraint at table level.
Syntax: SQL> create table <table.name> (column name1 <datatype> (size),
columnname.2<datatype> (size),…. ,
constraint <constraint_name> PRIMARY KEY (columnname1));
Query: SQL> create table student (rollno number(5) NOT NULL , dept char(5) , name
char(15), constraint stud PRIMARY KEY( rollno));
d) Specifying constraint with alter command:
Write queries to create primary key constraint at table level using alter command
Syntax: SQL> create table <tablename> (column name.1 <datatype> (size), column
name.2 <datatype> (size)…… );
SQL>alter table <table name> add constraint <constraint name> <constraint type>(column
name);
Query: SQL> create table student (rollno number(5) NOT NULL, dept char(5), name
char(15)); SQL> alter table student add constraint consrt PRIMARY KEY(rollno);
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

4. FOREIGN KEY: A FOREIGN KEY in one table points to a PRIMARY KEY in


another table. The FOREIGN KEY constraint is used to prevent actions that would
destroy the link between tables.The FOREIGN KEY constraint also prevents that
invalid data from being inserted into the foreign key column, because it has to be one
of the values contained in the table it points to.

a) Column level constraint:


Write a query to create foreign key constraint at column level.
Syntax:
Parent table: SQL> create table <table.name1> (column name.11<datatype> (size)
PRIMARY KEY, column name.12 <datatype> (size),…… column name 1n < data
type>(size));
Child table: SQL> create table <table.name2> (column name.1<datatype> (size) , column
name.2 <datatype> (size) FOREIGN KEY references <table name1> ( column name11));
Query:
Parent table: SQL> create table student (rollno number(5) PRIMARY KEY , dept
char(5), name char(15));
Child table: SQL> create table project (pno char(20) , studno number(5) FOREIGN KEY
references student (rollno));

b) Column level constraint with naming convention:


Write a query to create foreign key constraint at column level.
Syntax:
Parent table: SQL> create table <table.name1> (column name.11<datatype> (size)
constraint <constraint name> PRIMARY KEY, column name.12 <datatype> (size),……
column name 1n < data type>(size));
Child table: SQL> create table <table.name2> (column name.1<datatype> (size) , column
name.2 <datatype> (size) constraint <constraint name> FOREIGN KEY references <table
name1> ( column name11));
Query:
Parent table: SQL> create table student (rollno number(5) constraint consrt PRIMARY
KEY , dept char(5), name char(15));
Child table: SQL> create table project (pno char(20) , studno number(5) constraint consrt
FOREIGN KEY references student (rollno));

c) Table level constraint:


Write a query to create foreign key constraint at table level.
Syntax:
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Parent table: SQL> create table <table.name1> (column name11 <datatype> (size),
columnname.12<datatype> (size),…. ,PRIMARY KEY (columnname11));
Child table: SQL> create table <table.name2> (column name.1<datatype> (size) , column
name.2 <datatype> (size), constraint <constraint name> FOREIGN KEY(column name2)
references <table name1> ( column name11));
Query:
Parent table: SQL> create table student (rollno number(5) , dept char(5), name
char(15), PRIMARY KEY(rollno));
Child table: SQL> create table project (pno char(20) , studno number(5), constraint consrt
FOREIGN KEY (studno)references student (rollno));

d) Specifying constraint with alter command:


Write queries to create foreign key constraint at table level using alter command.
Syntax:
Parent table: SQL> create table <table.name1> (column name11 <datatype> (size),
columnname.12<datatype> (size),…. ,PRIMARY KEY (columnname11));
Child table: SQL> create table <tablename2> (column name.1 <datatype> (size),
column name.2 <datatype> (size)…… );
SQL>alter table <table name2> add constraint <constraint name> FOREIGN KEY(column
name2) references <table name1> ( column name11
Query:
Parent table: SQL> create table student (rollno number(5) PRIMARY KEY, dept char(5),
name char(15));
Child table: SQL> create table project (pno char(20) , studno number(5));
SQL> alter table project add constraint consrt FOREIGN KEY (studno)references student
(rollno));
5. CHECK: This constraint is used to limit the value range that can be placed in a
column.
a) Column level constraint:
Syntax: SQL> create table <table.name> (column name.1<datatype> (size) CHECK
(constraint criteria), column name.2 <datatype> (size),… column name n < data
type>(size));
Query: SQL> create table student (rollno number(5) CHECK ( rollno>0), name char(10));
b) Column level constraint with naming convention:
Syntax: SQL> create table <table.name> (column name.1<datatype> (size) constraint
<constraint name> CHECK (constraint criteria), column name.2 <datatype> (size),…
column name n < data type>(size));
Query: SQL> create table student (rollno number(5) constraint consrt CHECK ( rollno>0),
name char(10));
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Table level constraint:


c)
Syntax: SQL> create table <table.name> (column name.1<datatype> (size), column name.2
<datatype> (size),… column name n < data type>(size), constraint <constraint name>
CHECK (constraint criteria));
Query: SQL> create table student (rollno number(5) ,mark number(3), constraint consrt
CHECK (rollno>0 and mark>0));

d) Specifying constraint with alter command:


Syntax: SQL> create table <table.name> (column name.1<datatype> (size) ,
column name.2<datatype> (size) );
SQL>alter table <table name> add constraint <constraint name>
CHECK(column name with
criteria);
Query: SQL> create table student (rollno number(5) ,mark number(3));
SQL>alter table student add constraint consrt CHECK (rollno>0 and mark>0));

6. DEFAULT: The DEFAULT constraint is used to insert a default value into a


column.The default value will be added to all new records, if no other value is
specified.
a) Column level constraint:
Syntax: SQL> create table <table.name> (column name.1<datatype> (size) DEFAULT
(value), column name.2 <datatype> (size),… column name n < data type>(size));
Query: SQL> create table student(rollno number(5), mark number(3), college char(5)
DEFAULT ‘KLNCE’);
b) Specifying constraint with alter command:
Syntax: SQL> create table <table.name> (column name.1<datatype> (size) , column
name.2<datatype> (size) );
SQL>alter table <table name> modify <column name> DEFAULT ‘value’;
Query: SQL> create table student(rollno number(5), mark number(3), college char(5));
SQL> alter table student modify college ‘KLNCE’;

OPERATIONS ON CONSTRAINT:
1.Write a query to disable a constraint.
Syntax: SQL>alter table <Table name> DISABLE CONSTRAINT <constraint name>;
Query: SQL>alter table student DISABLE CONSTRAINT consrt;

2. Write a query to enable a constraint.


Syntax: SQL>alter table <Table name> ENABLE CONSTRAINT <constraint name>;
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Query: SQL>alter table student ENABLE CONSTRAINT consrt;

3. Write a query to drop a constraint.


Syntax: SQL> alter table <Table name> DROP CONSTRAINT <constraint name>;
Query: SQL> alter table student DROP CONSTRAINT consrt;
RESULT
Thus a set of SQL statements implementing DDL commands and Constraints were
executed successfully.

Ex. No: 4 Nested Subqueries


Aim:
To execute a set of SQL nested subqueries for banking database and employee
database manipulation.
Subquery:
SQL provides a mechanism for the nesting of subqueries. A subquery is a select-from-where
expression that is nested within another query.
I. Banking Database:
Table borrower: Table Depositor: Table Branch:

1. Find all customers who have both an account and a loan at the bank.
select distinct customer-name from borrower
where customer-name in (select customer-name from depositor)
2. Find all customers who have a loan at the bank but do not have
an account at the bank
select distinct customer-name from borrower
where customer-name not in (select customer-name from depositor)
3. Find the names of all branches that have greater assets than all branches located
in Brooklyn.
select branch-name from branch where assets > all
(select assets from branch where branch-city = ‘Brooklyn’)

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

II. Employee Database


Table emp:
Id name Job Salary Bonus_I Dept_no City
nc
1 aaa Developer 30000 y 10 Madurai
2 bbb Manager 32000 n 20 Chennai
3 ccc Tester 25000 n 10 Coimbatore
4 ddd Clerk 10000 n 30 Madurai
5 eee Debugger 30000 y 20 Chennai
2 fff manager 32000 y 10 Madurai

1. Provide a 8% raise in salary for the employees with no bonus increment.


update emp set salary = salary + (salary*0.08)
where id in ( select id from emp where bonus_inc=’n’);
2. Retrieve the details of the employee who earn the minimum salary.
Select * from emp where salary in ( select min(sal) from emp);
3. Find the employees whose designation is manager.
Select name,job from emp where id in ( select id from emp where job=’manager’);
4. Find the details of the employees who earn minimum salary in each department.
Select * from emp where (salary,dept_no)
in ( select min(salary), dept_no from emp group by dept_no);
5. List the number of designations available.
Select count(job) as count from ( select job from emp group by job);

Result:
Thus a set of SQL nested subqueries for manipulating banking database and employee
database were executed successfully.

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

JOIN QUERIES
Aim
To execute a set of SQL queries implementing joins.

Table Books : Table


Purchase :
Book_id Title Author Publisher Book_id Price Store
B1 Database Silberchatz McGraw B2 300 A
Hill
B2 Data Structures Weiss Pearson B8 300 B
B3 Compilers Ullman Pearson B3 350 A
B4 Multimedia Thakrar Pearson B4 360 C
B5 Operating Silberchatz Wiley B7 360 A
Systems

SQL Joins:
SQL joins are used to query data from two or more tables, based on a relationship between
certain columns in these tables.

1. INNER JOIN: Return rows when there is at least one match in both tables
Write a query to display the books details with their purchase details with no null values.
Syntax: SQL> SELECT column_name(s) FROM table_name1 INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name;
Query: SQL> select * from books INNER JOIN purchase on
books.book_id=purchase.book_id order by books.title;

2. LEFT JOIN: Return all rows from the left table, even if there are no matches in the
right table.
Write a query to display all the available book details with or without their purchase details.
Syntax: SQL> SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON
table_name1.column_name=table_name2.column_name;
Query: SQL> select * from books LEFT JOIN purchase on
books.book_id=purchase.book_id order by books.title;

3. RIGHT JOIN: Return all rows from the right table, even if there are no matches in the
left table
Write a query to display all the available purchase details with or without their book details.

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

Syntax: SQL> SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2


ON table_name1.column_name=table_name2.column_name;
Query: SQL> select * from books RIGHT JOIN purchase on
books.book_id=purchase.book_id order by books.title;

4. FULL JOIN: Return rows when there is a match in one of the tables
Write a query to display all the available books details with all the available purchase details.
Syntax: SQL> SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON
table_name1.column_name=table_name2.column_name;
Query: SQL> select * from books RIGHT JOIN purchase on
books.book_id=purchase.book_id order by books.title;

Result
Thus a set of join queries were executed successfully.

SET OPERATIONS:
Aim
To execute a set of SQL statements implementing set operations.
TABLE PROJECTA :
select * from projecta;

TABLE PROJECTB:
select * from projectb;

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES

SET OPERATIONS:

1.UNION ALL: Merges the output of two or more queries into a single set of rows and
columns with repetition.
Write a query to display all the employees who work for either projecta or projectb.
Syntax: select (column_name 1,...,column_name n) from <table_name>
union all
select (column_name 1,...,column_name n) from <table_name> ;
Query: select ename from projecta union all select ename from projectb;

2.UNION : Merges the output of two or more queries into a single set of rows and columns.
Write a query to display the employees who work for either projecta or project.
Syntax: select (column_name 1,...,column_name n) from <table_name>
union
select (column_name 1,...,column_name n) from <table_name> ;
Query: select ename from projecta union select ename from projectb;

2.INTERSECT: Outputs the rows resulted by both the queries.


Write a query to display the employees who work for both projecta and projectb.
Syntax: select (column_name 1,...,column_name n) from <table_name>
intersect
select (column_name 1,...,column_name n) from <table_name> ;
Query: select ename from projecta intersect select ename from projectb;

3.MINUS : Outputs the rows produced by first query, after filtering the rows retrieved by the
second query.
Write a query to display the employees who work only for projecta.
Syntax: select (column_name 1,...,column_name n) from <table_name>
Minus
select (column_name 1,...,column_name n) from <table_name> ;
Query: select ename from projecta minus select ename from projectb;
Result:
Thus a set of SQL statements implementing set operations were executed successfully.

www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus

© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com

Das könnte Ihnen auch gefallen