Dvma Notes With QP Pattern
Dvma Notes With QP Pattern
Example (Column):
The lastname column stores employee last names
Each column stores a specific type of data for all rows.
lastname
Rai
Roy
Roy
A database-management system (DBMS) is a collection of interrelated data and a set of programs to access those data. The collection of data, usually
referred to as the database, contains information relevant to an enterprise. The primary goal of a DBMS is to provide a way to store and retrieve
database information that is both convenient and efficient. (**you can also refer definition from notebook)
Database System Applications
Databases are widely used. Here are some representative applications:
• Banking: For customer information, accounts, and loans, and banking transactions.
• Airlines: For reservations and schedule information. Airlines were among the first to use databases in a geographically distributed manner—
terminals situated around the world accessed the central database system through phone lines and other data networks.
• Universities: For student information, course registrations, and grades.
• Credit card transactions: For purchases on credit cards and generation of monthly statements.
• Telecommunication: For keeping records of calls made, generating monthly bills, maintaining balances on prepaid calling cards, and storing
information about the communication networks.
• Finance: For storing information about holdings, sales, and purchases of financial instruments such as stocks and bonds.
• Sales: For customer, product, and purchase information.
• Manufacturing: For management of supply chain and for tracking production of items in factories, inventories of items in warehouses/stores, and
orders for items.
• Human resources: For information about employees, salaries, payroll taxes and benefits, and for generation of pay checks.
View of Data
A database system is a collection of interrelated files and a set of programs that allow users to access and modify these files. A major purpose of a
database system is to provide users with an abstract view of the data. That is, the system hides certain details of how the data are stored and
maintained.
Data Abstraction
For the system to be usable, it must retrieve data efficiently. The need for efficiency has led designers to use complex data structures to represent data
in the database. Since many database-systems users are not computer trained, developers hide the complexity from users through several levels of
abstraction, to simplify users’ interactions with the system:
• Physical level. The lowest level of abstraction describes how the data are actually stored. The physical level describes complex low-level data
structures in detail.
• Logical level. The next-higher level of abstraction describes what data are stored in the database, and what relationships exist among those data.
The logical level thus describes the entire database in terms of a small number
of relatively simple structures. Although implementation of the simple structures at the logical level may involve complex physical-level structures,
the user of the logical level does not need to be aware of this complexity. Database
administrators, who must decide what information to keep in the database, use the logical level of abstraction.
View level. The highest level of abstraction describes only part of the entire database. Even though the logical level uses simpler structures, complexity
remains because of the variety of information stored in a large database. Many users of the database system do not need all this information; instead,
they need to access only a part of the database. The view level of abstraction exists to simplify their interaction with the system. The system may
provide many views for the same database.
An attribute, as used in the E-R model, can be characterized by the following attribute types:
Simple and composite attributes. In our examples thus far, the attributes have been simple; that is, they are not divided into subparts. Composite
attributes, on the other hand, can be divided into subparts (that is, other attributes). For example, an attribute name could be structured as a composite
attribute consisting of first-name, middle-initial, and last-name.
Single-valued and multivalued attributes. The attributes in our examples all have a single value for a particular entity. For instance, the loan-number
attribute for a specific loan entity refers to only one loan number. Such attributes are said to be single valued. There may be instances where an
attribute has a set of values for a specific entity. Consider an employee entity set with the attribute phone-number.
Derived attribute. The value for this type of attribute can be derived from the values of other related attributes or entities. For instance, if the customer
entity set also has an attribute date-of-birth, we can calculate age from date-of-birth and the current date.
Specialization
The refinement from an initial entity set into successive levels of entity subgroupings represents a top-down design process in which distinctions are
made explicit. Consider an entity set person, with attributes name, street, and city.
The process of designating subgroupings within an entity set is called specialization.
As another example, suppose the bank wishes to divide accounts into two categories, checking account and savings account. Savings accounts need
a minimum balance, but the bank may set interest rates differently for different customers, offering better rates to favoured customers. Checking
accounts have a fixed interest rate, but offer an overdraft facility; the overdraft amount on a checking account must be recorded. The bank could then
create two specializations of account, namely savings-account and checking-account.
Generalization
The design process may also proceed in a bottom-up manner, in which multiple entity sets are synthesized into a higher-level entity set on the basis
of common features. The database designer may have first identified a customer entity set with the attributes name, street, city, and customer-id, and an
employee entity set with the attributes name, street, city, employee-id, and salary. There are similarities between the customer entity set and the employee
entity set in the sense that they have several attributes in common. This commonality can be expressed by generalization, which is a containment
relationship that exists between a higher-level entity set and one or more lower-level entity sets.
Constraints on Generalizations
The lower-level entity sets may be one of the following:
• Disjoint. A disjointness constraint requires that an entity belong to no more than one lower-level entity set. In our example, an account entity can
satisfy only one condition for the account-type attribute; an entity can be either a savings
account or a checking account, but cannot be both.
• Overlapping. In overlapping generalizations, the same entity may belong to more than one lower-level entity set within a single generalization. For
an illustration, consider the employee work team example, and assume that certain
managers participate in more than one work team. A given employee may therefore appear in more than one of the team entity sets that are lower-
level entity sets of employee. Thus, the generalization is overlapping.
Cardinality:
Mapping cardinalities, or cardinality ratios, express the number of entities to which another entity can be associated via a relationship set. Mapping
cardinalities are most useful in describing binary relationship sets, although they can contribute to the description of relationship sets that involve
more than two entity sets. In this section, we shall concentrate on only binary relationship sets.
For a binary relationship set R between entity sets A and B, the mapping cardinality must be one of the following:
• One to one. An entity in A is associated with at most one entity in B, and an entity in B is associated with at most one entity in A. (See Figure 2.4a.)
• One to many. An entity in A is associated with any number (zero or more) of entities in B. An entity in B, however, can be associated with at most
one entity in A. (See Figure 2.4b.)
• Many to one. An entity in A is associated with at most one entity in B. An entity in B, however, can be associated with any number (zero or more)
of entities in A. (See Figure 2.5a.)
• Many to many. An entity in A is associated with any number (zero or more) of entities in B, and an entity in B is associated with any number (zero
or more) of entities in A. (See Figure 2.5b.)
Database Administrator
One of the main reasons for using DBMSs is to have central control of both the data and the programs that access those data. A person who has such
central control over the system is called a database administrator (DBA). The functions of a DBA include:
• Schema definition. The DBA creates the original database schema by executing a set of data definition statements in the DDL.
• Storage structure and access-method definition.
• Schema and physical-organization modification. The DBA carries out changes to the schema and physical organization to reflect the changing
needs of the organization, or to alter the physical organization to improve performance.
• Granting of authorization for data access. By granting different types of authorization, the database administrator can regulate which parts of the
database various users can access. The authorization information is kept in a special system structure that the database system consults whenever
someone attempts to access the data in the system.
• Routine maintenance. Examples of the database administrator’s routine maintenance activities are:
Periodically backing up the database, either onto tapes or onto remote servers, to prevent loss of data in case of disasters such as flooding.
Ensuring that enough free disk space is available for normal operations, and upgrading disk space as required.
Monitoring jobs running on the database and ensuring that performance is not degraded by very expensive tasks submitted by some users.
Storage Manager
A storage manager is a program module that provides the interface between the low level data stored in the database and the application programs
and queries submitted to the system. The storage manager is responsible for the interaction with the file manager. The raw data are stored on the disk
using the file system, which is usually provided by a conventional operating system. The storage manager translates the various DML statements
into low-level file-system commands. Thus, the storage manager is responsible for storing, retrieving, and updating data in the database.
The storage manager components include:
• Authorization and integrity manager, which tests for the satisfaction of integrity constraints and checks the authority of users to access data.
• Transaction manager, which ensures that the database remains in a consistent (correct) state despite system failures, and that concurrent transaction
executions proceed without conflicting.
• File manager, which manages the allocation of space on disk storage and the data structures used to represent information stored on disk.
• Buffer manager, which is responsible for fetching data from disk storage into main memory, and deciding what data to cache in main memory. The
buffer manager is a critical part of the database system, since it enables the database
to handle data sizes that are much larger than the size of main memory.
The storage manager implements several data structures as part of the physical system implementation:
• Data files, which store the database itself.
• Data dictionary, which stores metadata about the structure of the database, in particular the schema of the database.
• Indices, which provide fast access to data items that hold particular values.
Customer Table
customer_id (PK) customer_name customer_street customer_city
Branch Table
branch_name (PK) branch_city assets
Loan Table
loan_number (PK) branch_name (FK) amount
Account Table
account_number (PK) branch_name (FK) balance
Employee Table
employee_id (PK) employee_name telephone_number start_date
Depositor Table
customer_id (PK, FK) account_number (PK, FK) access_date
Checking_Account Table
account_number (PK, FK) overdraft_amount
Customer Table
CREATE TABLE Customer (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(50),
customer_street VARCHAR(100),
customer_city VARCHAR(50)
);
Branch Table
CREATE TABLE Branch (
branch_name VARCHAR(50) PRIMARY KEY,
branch_city VARCHAR(50),
assets DECIMAL(15,2)
);
Loan Table
CREATE TABLE Loan (
loan_number INT PRIMARY KEY,
branch_name VARCHAR(50),
amount DECIMAL(15,2),
FOREIGN KEY (branch_name) REFERENCES Branch(branch_name)
);
Account Table
CREATE TABLE Account (
account_number INT PRIMARY KEY,
branch_name VARCHAR(50),
balance DECIMAL(15,2),
FOREIGN KEY (branch_name) REFERENCES Branch(branch_name)
);
Employee Table
CREATE TABLE Employee (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
telephone_number VARCHAR(20),
start_date DATE
);
Depositor Table
CREATE TABLE Depositor (
customer_id INT,
account_number INT,
access_date DATE,
PRIMARY KEY (customer_id, account_number),
FOREIGN KEY (customer_id) REFERENCES Customer(customer_id),
FOREIGN KEY (account_number) REFERENCES Account(account_number)
);
Constraints
1. A PRIMARY KEY uniquely identifies each record (row) in a table.
It cannot have duplicate or NULL values.
2. A FOREIGN KEY establishes a relationship between two tables.
It ensures referential integrity, meaning the foreign key value must exist in the referenced table.
3. The UNIQUE constraint ensures all values in a column are different (no duplicates).
Unlike PRIMARY KEY, it allows NULL values.
4. The NOT NULL constraint ensures that a column cannot store NULL values.
5. The CHECK constraint ensures that a column follows a specific condition.
6. The DEFAULT constraint assigns a default value if no value is provided.
OUTPUT (DEFAULT):
insert into emp values( 7872, null, '', default, 7788,2)
OUTPUT (UNIQUE):
insert into emp values( 7872, null, '','CLERK', 7788,2)
ORA-00001: unique constraint (SYSTEM.SYS_C004107) violated
OUTPUT (CHECK):
insert into emp values(7876,'ADAM','','CLERK', 7788,2)
ORA-02290: check constraint (SYSTEM.SYS_C004109) violated
DESC DEPT
INSERT ALL
into DEPT (DEPTNO, DNAME, LOC) values (10, 'ACCOUNTING', 'NEW YORK')
into DEPT (DEPTNO, DNAME, LOC) values(20, 'RESEARCH', 'DALLAS')
into DEPT (DEPTNO, DNAME, LOC)values(30, 'SALES', 'CHICAGO')
into DEPT (DEPTNO, DNAME, LOC)values(40, 'FINANCE', 'BOSTON')
into DEPT (DEPTNO, DNAME, LOC) values(50, 'MANAGEMENT', 'BOSTON')
into DEPT (DEPTNO, DNAME, LOC)values(60, 'LAW', 'BOSTON')
into DEPT (DEPTNO, DNAME, LOC)values(70, 'ADMINISTRATION', 'BOSTON')
SELECT * FROM dual;
DATE FUNCTIONS
ADD_MONTHS, MONTHS_BETWEEN, CURRENT_DATE, SYSDATE, LAST_DAY, NEXT_DAY
TO_CHAR, EXTRACT, TO_DATE
create table for_datefunctions_demo
(empid int primary key,
empname varchar(20),
hiredate date);
select empname,sysdate,hiredate,
trunc(months_between(sysdate,hiredate)/12)as years,
trunc(months_between(sysdate,hiredate)-
(trunc(months_between(sysdate,hiredate)/12)*12))as months
from for_datefunctions_demo
select empname,
to_char(trunc(hiredate,'cc'),'dd-mm-yyyy')century,
to_char(trunc(hiredate,'y'),'dd-mm-yyyy')year,
to_char(trunc(hiredate,'q'),'dd-mm-yyyy')quarter,
to_char(trunc(hiredate,'w'),'dd-mm-yyyy')dayofweek,
to_char(trunc(hiredate,'d'),'dd-mm-yyyy')closestsunday
from for_datefunctions_demo
select empname,hiredate from for_datefunctions_demo where hiredate between date '1988-12-01'and date '2016-12-31'
select empname,hiredate, extract(year from hiredate)year, extract(month from hiredate)month,extract(day from hiredate)day from
for_datefunctions_demo
NUMERIC FUNCTIONS
MOD, LOG, POWER, ABS, SQRT, ROUND, EXP, GREATEST, LEAST, TRUNC, CEIL, TO_NUMBER
select greatest(10,20,30) MAX, least(10,20,30) MIN from dual;
SELECT MOD(7,2) FROM DUAL;
MOD(7,2)
1
LOG(10,1000)
3
POWER(4,-2)
.0625
POWER(2,4)
16
ABSOLUTE_VALUE
20
SQRT(4)
2
POWER(2,2)
4
MOD(5,2)
1
LEAST(5,2,0)
0
TRUNC(59.9)
59
SELECT CEIL(59.1)FROM DUAL;
CEIL(59.1)
60
ROUND(59.9)
60
ROUND(59.1)
59
SELECT * FROM for_datefunctions_demo WHERE EMPNAME = 'dazy' AND HIREDATE > '1-JUN-2000'
Output:
EMPID EMPNAME HIREDATE
1 dazy 12-JUN-02
INSERT ALL
into DEPT (DEPTNO, DNAME, LOC) values (10, 'ACCOUNTING', 'NEW YORK')
into DEPT (DEPTNO, DNAME, LOC) values(20, 'RESEARCH', 'DALLAS')
into DEPT (DEPTNO, DNAME, LOC)values(30, 'SALES', 'CHICAGO')
into DEPT (DEPTNO, DNAME, LOC)values(40, 'FINANCE', 'BOSTON')
into DEPT (DEPTNO, DNAME, LOC) values(50, 'MANAGEMENT', 'BOSTON')
into DEPT (DEPTNO, DNAME, LOC)values(60, 'LAW', 'BOSTON')
into DEPT (DEPTNO, DNAME, LOC)values(70, 'ADMINISTRATION', 'BOSTON')
SELECT * FROM dual;
INSERT ALL
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7839, 'KING', 'PRESIDENT', null, to_date('17-11-1981','dd-mm-yyyy'),
5000, null, 10 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7782, 'CLARK', 'MANAGER', 7839, to_date('9-6-1981','dd-mm-yyyy'),
2450, null, 10 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7844, 'TURNER', 'SALESMAN', 7698, to_date('8-9-1981','dd-mm-yyyy'),
1500, 0, 10 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7934, 'MILLER', 'CLERK', 7782, to_date('23-1-1982','dd-mm-yyyy'), 1300,
null, 20 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7566, 'JONES', 'MANAGER', 7839, to_date('2-4-1981','dd-mm-yyyy'), 2975,
null, 20 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7788, 'SCOTT', 'ANALYST', 7566, to_date('13-JUL-87','dd-mm-rr') - 85,
3000, null, 20 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7902, 'FORD', 'ANALYST', 7566, to_date('3-12-1981','dd-mm-yyyy'), 3000,
null, 20 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7876, 'ADAMS', 'CLERK', 7788, to_date('13-JUL-87', 'dd-mm-yyyy'), 1100,
null, 20 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 764, 'MARTIN', 'SALESMAN', 7698, to_date('28-9-1981','dd-mm-yyyy'),
1250, 1400, 30 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7900, 'JAMES', 'CLERK', 7698, to_date('3-12-1981','dd-mm-yyyy'), 950,
null, 30 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7499, 'ALLEN', 'SALESMAN', 7698, to_date('20-2-1981','dd-mm-yyyy'),
1600, 300, 30 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 784, 'TURNER', 'SALESMAN', 7698, to_date('8-9-1981','dd-mm-yyyy'),
1500, 0, 40 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7698, 'BLAKE', 'MANAGER', 7839, to_date('1-5-1981','dd-mm-yyyy'), 2850,
null, 40 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7521, 'WARD', 'SALESMAN', 7698, to_date('22-2-1981','dd-mm-yyyy'),
1250, 500, 50 )
into emp(empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 794, 'MILLER', 'CLERK', 7782, to_date('23-1-1982','dd-mm-yyyy'), 1300, '',
50 )
into emp(empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 790, 'JAMES', 'CLERK', 7698, to_date('3-12-1981','dd-mm-yyyy'), 950, '', 60 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7654, 'MARTIN', 'SALESMAN', 7698, to_date('28-9-1981','dd-mm-yyyy'),
1250, 1400, 60 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 786, 'ADAMS', 'CLERK', 7788, to_date('13-JUL-87', 'dd-mm-yyyy'), 1100, '',
70 )
into emp( empno,ename ,job ,mgr , hiredate, sal , comm , deptno) values( 7369, 'SMITH', 'CLERK', 7902, to_date('17-12-1980','dd-mm-yyyy'), 800,
null, 70 )
select ename, dname, job, empno, hiredate, loc from emp, dept where emp.deptno = dept.deptno order by ename;
AGGREGATE FUNCTIONS (SUM, MAX, MIN, COUNT, AVG),
groupby, having, order by,
PATTERN MATCHING (%, _ , LIKE, NOT LIKE)
select job,max(sal),min(sal),sum(sal),avg(sal),count(*) No_of_Employees from emp , dept where emp.deptno = dept.deptno group by job
select dname,max(sal),min(sal),sum(sal),avg(sal),count(*) No_of_Employees from emp , dept where emp.deptno = dept.deptno group by Dname
SELECT * FROM DEPT WHERE DNAME NOT LIKE '_E%' and DNAME NOT LIKE '_I%'
SELECT * FROM DEPT WHERE DNAME LIKE '_E%' or DNAME LIKE '_I%'
SELECT * FROM DEPT WHERE CustomerName LIKE 'a%'
Above query Finds any values that start with "a"
SELECT * FROM DEPT WHERE CustomerName LIKE '%a'
Above query Finds any values that end with "a"
SELECT * FROM DEPT WHERE CustomerName LIKE '%or%'
Above query Finds any values that have "or" in any position
SELECT * FROM DEPT WHERE CustomerName LIKE '_r%'
Above query Finds any values that have "r" in the second position
SELECT * FROM DEPT WHERE CustomerName LIKE 'a__%'
Above query Finds any values that start with "a" and are at least 3 characters in length
SELECT * FROM DEPT WHERE ContactName LIKE 'a%o'
Above query Finds any values that start with "a" and ends with "o"
insert into DEPT (DEPTNO, DNAME, LOC) values(80, 'ACCOUNTING_try_inserting', 'NEW YORK');
SELECT * from dept
UPDATE Customers
SET LOC = 'TEXAS'
WHERE DEPTNO = 80;
STRING FUNCTIONS
LOWER, UPPER, INITCAP, ASCII, LENGTH, CONCAT, || ' ' ||, SUBSTR, TRIM, LENGTH(TRIM()) , LTRIM, RTRIM, LPAD, RPAD,
TRANSLATE, INSTR
SELECT DNAME, DNAME || ' ' || ' Designation:' || LOC FULLNAME FROM DEPT WHERE DEPTNO = 1;
select substr(DNAME,0,6) DNAME, substr(DNAME,13,7) DNAME, substr(DNAME,6,8) DNAME FROM DEPT WHERE DEPTNO = 1;
SELECT DNAME, length(DNAME), TRIM(DNAME) TRIMMED_DNAME, length(TRIM(DNAME)) TRIMMED_DNAME FROM DEPT WHERE
DEPTNO = 1;
SELECT d.department_name,
e.employee_name
FROM employees e, departments d
WHERE e.department_id = d.department_id (+)
UNION ALL
SELECT d.department_name,
e.employee_name
FROM departments d, employees e
WHERE d.department_id = e.department_id (+)
AND e.employee_name IS NULL
ORDER BY 1, 2;
DEPARTMENT_NAME EMPLOYEE_NAME
ACCOUNTING CLARK
ACCOUNTING KING
ACCOUNTING MILLER
OPERATIONS -
RESEARCH ADAMS
RESEARCH FORD
RESEARCH SCOTT
SALES ALLEN
SALES BLAKE
SALES JAMES
SALES MARTIN
SALES TURNER
SALES WARD
- JONES
Remember, the INNER keyword is optional. In the examples below, we are returning the DEPARTMENT_NAME and the EMPLOYEE_NAME for
each employee. The OPERATIONS department has a DEPARTMENT_ID of 40, so it is not removed by the filter condition, but there are no
employees in this department, so there is no match and it is not returned in the result set.
SELECT d.department_name,
e.employee_name
FROM departments d
JOIN employees e ON d.department_id = e.department_id
WHERE d.department_id >= 30
ORDER BY d.department_name;
DEPARTMENT_NAME EMPLOYEE_NAME
SALES ALLEN
SALES WARD
SALES TURNER
SALES BLAKE
SALES JAMES
SALES MARTIN
Using the previous example, but switching to a LEFT OUTER JOIN means we will see the OPERATIONS department, even though it has no
employees.
SELECT d.department_name,
e.employee_name
FROM departments d
LEFT OUTER JOIN employees e ON d.department_id = e.department_id
WHERE d.department_id >= 30
ORDER BY d.department_name, e.employee_name;
DEPARTMENT_NAME EMPLOYEE_NAME
OPERATIONS -
SALES ALLEN
SALES BLAKE
SALES JAMES
SALES MARTIN
SALES TURNER
SALES WARD
The following example has altered the order of the tables so a RIGHT [OUTER] JOIN is now required.
SELECT d.department_name,
e.employee_name
FROM employees e
RIGHT OUTER JOIN departments d ON e.department_id = d.department_id
WHERE d.department_id >= 30
ORDER BY d.department_name, e.employee_name;
DEPARTMENT_NAME EMPLOYEE_NAME
OPERATIONS -
SALES ALLEN
SALES BLAKE
SALES JAMES
SALES MARTIN
SALES TURNER
SALES WARD
To see a working example, we need to add another employee who is not assigned to a department.
INSERT INTO employees VALUES (8888,'JONES','DBA',null,to_date('02-1-1982','dd-mm-yyyy'),1300,NULL,NULL);
DEPARTMENT_NAME EMPLOYEE_NAME
ACCOUNTING CLARK
ACCOUNTING KING
ACCOUNTING MILLER
OPERATIONS -
RESEARCH ADAMS
RESEARCH FORD
RESEARCH SCOTT
SALES ALLEN
SALES BLAKE
SALES JAMES
SALES MARTIN
SALES TURNER
SALES WARD
- JONES
CROSS JOIN
A CROSS JOIN is the deliberate creation of a Cartesian product. There are no join columns specified, so every possible combination of rows
between the two tables is produced.
EMPLOYEE_NAME DEPARTMENT_NAME
ADAMS ACCOUNTING
ADAMS OPERATIONS
ADAMS RESEARCH
ADAMS SALES
ALLEN ACCOUNTING
ALLEN OPERATIONS
ALLEN RESEARCH
ALLEN SALES
BLAKE ACCOUNTING
BLAKE OPERATIONS
BLAKE RESEARCH
BLAKE SALES
CLARK ACCOUNTING
CLARK OPERATIONS
CLARK RESEARCH
CLARK SALES
FORD ACCOUNTING
FORD OPERATIONS
FORD RESEARCH
FORD SALES
JAMES ACCOUNTING
JAMES OPERATIONS
JAMES RESEARCH
JAMES SALES
JONES ACCOUNTING
JONES OPERATIONS
JONES RESEARCH
JONES SALES
KING ACCOUNTING
KING OPERATIONS
KING RESEARCH
KING SALES
MARTIN ACCOUNTING
MARTIN OPERATIONS
MARTIN RESEARCH
MARTIN SALES
MILLER ACCOUNTING
MILLER OPERATIONS
MILLER RESEARCH
MILLER SALES
SCOTT ACCOUNTING
SCOTT OPERATIONS
SCOTT RESEARCH
SCOTT SALES
TURNER ACCOUNTING
TURNER OPERATIONS
TURNER RESEARCH
TURNER SALES
WARD ACCOUNTING
WARD OPERATIONS
WARD RESEARCH
WARD SALES
NATURAL JOIN
A NATURAL JOIN is a variant on an INNER JOIN. The join columns are determined implicitly, based on the column names. Any columns that
share the same name between the two tables are assumed to be join columns. Here is an example using the ANSI join syntax.
SELECT e.employee_name,
d.department_name
FROM employees e
NATURAL JOIN departments d
ORDER BY e.employee_name, d.department_name;
EMPLOYEE_NAME DEPARTMENT_NAME
ADAMS RESEARCH
ALLEN SALES
BLAKE SALES
CLARK ACCOUNTING
FORD RESEARCH
JAMES SALES
KING ACCOUNTING
MARTIN SALES
MILLER ACCOUNTING
SCOTT RESEARCH
TURNER SALES
WARD SALES
NESTED QUERIES:
DISPLAY THE EMPLOYEE NUMBER AND NAME OF EMPLOYEE WORKING AS CLERK AND EARNING HIGHEST SALARY AMONG
CLERKS.
SELECT EMPNO, ENAME FROM EMP WHERE JOB='CLERK' AND SAL=(SELECT MAX(SAL) FROM EMP WHERE JOB='CLERK');
DISPLAY THE NAMES OF THE SALESMAN WHO EARNS A SALARY MORE THAN THE HIGHEST SALARY OF ANY CLERK.
SELECT ENAME FROM EMP WHERE JOB=’SALESMAN’ AND SAL > (SELECT MAX(SAL) FROM EMP WHERE JOB='CLERK');
DISPLAY THE NAMES OF CLERKS WHO EARN SALARY MORE THAN THAT OF JAMES OF THAT OF SAL LESSER THAN THAT OF
SCOTT
SELECT ENAME FROM EMP WHERE JOB='CLERK' AND SAL<(SELECT SAL FROM EMP WHERE ENAME='SCOTT') AND SAL>(SELECT SAL
FROM EMP WHERE ENAME='JAMES');
DISPLAY THE NAMES OF EMPLOYEES WHO EARN A SAL MORE THAN THAT OF JAMES OR THAT OF SALARY GREATER THAN THAT
OF SCOTT.
SELECT ENAME FROM EMP WHERE SAL < (SELECT SAL FROM EMP WHERE ENAME='SCOTT') AND SAL > (SELECT SAL FROM EMP WHERE
ENAME='JAMES');
DISPLAY THE NAMES OF THE EMPLOYEES WHO EARN HIGHEST SALARY IN THEIR RESPECTIVE DEPARTMENTS.
SELECT * FROM EMP E WHERE SAL = (SELECT MAX(SAL) FROM EMP WHERE DEPTNO=E.DEPTNO)
DISPLAY THE NAMES OF EMPLOYEES WHO EARN HIGHEST SALARIES IN THEIR RESPECTIVE JOB GROUPS.
SELECT * FROM EMP E WHERE SAL IN (SELECT MAX(SAL) FROM EMP GROUP BY JOB HAVING E.JOB=JOB)
DISPLAY THE JOB GROUPS HAVING TOTAL SALARY GREATER THEN THE MAXIMUM SALARY FOR MANAGERS.
SELECT JOB, SUM(SAL) FROM EMP GROUP BY JOB HAVING SUM(SAL) > (SELECT MAX(SAL) FROM EMP WHERE JOB='MANAGER');
QP PATTERN
SET A SET B
1. Attempt any one out of two [10] 1. Attempt any one out of two [10]
A. Theory A. Theory
OR OR
B. Queries + Thoery B. Theory
2. Attempt any one out of two [10] 2. Attempt any one out of two [10]
A. Theory A. Queries
OR OR
B. Queries B. Queries
3. Attempt any one out of two [10] 3. Attempt any one out of two [10]
A. Queries A. Theory
OR OR
B. Queries B. Queries
4. Attempt any one out of two [10] 4. Attempt any one out of two [10]
A. Queries A. Theory
OR OR
B. Queries B. Queries [10]
5. Attempt any one out of two [10] 5. Attempt any one out of two [10]
A. Theory A. Queries
OR OR
B. Theory + Queries B. Theory + Queries