Data Definition Language (DDL)
Data Definition Language (DDL)
Which is the subset of SQL commands used to manipulate Oracle Database structures, including tables?
Data Definition Language (DDL)
2.
LIKE operator
3.
4.
5.
What is the parameter substitution symbol used with INSERT INTO command?
&
6.
Which command displays the SQL command in the SQL buffer, and then executes it?
RUN
7.
8.
9.
State true or false. !=, <>, ^= all denote the same operation.
True
10. What are the privileges that can be granted on a table by a user to others?
Insert, update, delete, select, references, index, execute, alter, all
11. What command is used to get back the privileges offered by the GRANT command?
REVOKE
12. Which system tables contain information on privileges granted and privileges obtained?
USER_TAB_PRIVS_MADE, USER_TAB_PRIVS_RECD
13. Which system table contains information on constraints on all the tables created?
USER_CONSTRAINTS
14.
Explanation :
To copy only the structure, the WHERE clause of the SELECT command should contain a FALSE statement as
in the following.
CREATE TABLE NEWTABLE AS SELECT * FROM EXISTINGTABLE WHERE 1=2;
If the WHERE condition is true, then all the rows or rows satisfying the condition will be copied to the new table.
17. What will be the output of the following query?
20. Which date function is used to find the difference between two dates?
MONTHS_BETWEEN
21. Why does the following command give a compilation error?
DROP TABLE &TABLE_NAME;
Variable names should start with an alphabet. Here the table name starts with an '&' symbol.
22. What is the advantage of specifying WITH GRANT OPTION in the GRANT command?
The privilege receiver can further grant the privileges he/she has obtained from the owner to any other user.
23. What is the use of the DROP option in the ALTER TABLE command?
It is used to drop constraints specified on the table.
24. What is the value of comm and sal after executing the following query if the initial value of sal is 10000?
UPDATE EMP SET SAL = SAL + 1000, COMM = SAL*0.1;
sal = 11000, comm = 1000
25. What is the use of DESC in SQL?
Answer :
DESC has two purposes. It is used to describe a schema as well as to retrieve rows from table in descending
order.
Explanation :
The query SELECT * FROM EMP ORDER BY ENAME DESC will display the output sorted on ENAME in
descending order.
26. What is the use of CASCADE CONSTRAINTS?
When this clause is used with the DROP command, a parent table can be dropped even when a child table
exists.
27. Which function is used to find the largest integer less than or equal to a specific value?
FLOOR
28. What is the output of the following query?
SELECT TRUNC(1234.5678,-2) FROM DUAL;
1200
SQL QUERIES
I. SCHEMAS
Table 1 : STUDIES
PNAME (VARCHAR), SPLACE (VARCHAR), COURSE (VARCHAR), CCOST (NUMBER)
Table 2 : SOFTWARE
PNAME (VARCHAR), TITLE (VARCHAR), DEVIN (VARCHAR), SCOST (NUMBER), DCOST (NUMBER), SOLD
(NUMBER)
Table 3 : PROGRAMMER
PNAME (VARCHAR), DOB (DATE), DOJ (DATE), SEX (CHAR), PROF1 (VARCHAR), PROF2 (VARCHAR), SAL
(NUMBER)
LEGEND :
PNAME Programmer Name, SPLACE Study Place, CCOST Course Cost, DEVIN Developed in, SCOST
Software Cost, DCOST Development Cost, PROF1 Proficiency 1
QUERIES :
Find out the selling cost average for packages developed in Oracle.
Display the names of those who have done the PGDCA course.
Display the names and date of birth of all programmers born in April.
How much revenue has been earned through the sale of packages developed in C.
Display the details of packages whose sales crossed the 5000 mark.
Find out the number of copies which should be sold in order to recover the development cost of each
package.
Display the details of packages for which the development cost has been recovered.
Calculate the experience in years for each programmer and display along with their names in
descending order.
Who are the programmers who celebrate their birthdays during the current month?
Programmer
Mr. Arvind has 15 years of experience
KEYS:
SELECT
PNAME,TRUNC(MONTHS_BETWEEN(SYSDATE,DOB)/12)
"AGE",
SELECT COUNT(*) FROM STUDIES WHERE CCOST BETWEEN 10000 AND 15000;
SELECT * FROM PROGRAMMER WHERE PROF1 NOT IN ('C','C++') AND PROF2 NOT IN ('C','C++');
SELECT COUNT(*) FROM PROGRAMMER WHERE SAL BETWEEN 5000 AND 7500;
SELECT * FROM PROGRAMMER WHERE PROF1 NOT IN ('C','C++','PASCAL') AND PROF2 NOT IN ('C','C+
+','PASCAL');
32.SELECT 'Mr.' || PNAME || ' - has ' || TRUNC(MONTHS_BETWEEN(SYSDATE,DOJ)/12) || ' years of experience'
Programmer FROM PROGRAMMER WHERE SEX = 'M' UNION SELECT 'Ms.' || PNAME || ' - has ' || TRUNC
(MONTHS_BETWEEN (SYSDATE,DOJ)/12) || ' years of experience' Programmer FROM PROGRAMMER WHERE
SEX = 'F';
II . SCHEMA :
Table 1 : DEPT
DEPTNO (NOT NULL , NUMBER(2)), DNAME (VARCHAR2(14)),
LOC (VARCHAR2(13)
Table 2 : EMP
EMPNO (NOT NULL , NUMBER(4)), ENAME (VARCHAR2(10)),
JOB (VARCHAR2(9)), MGR (NUMBER(4)), HIREDATE (DATE),
SAL (NUMBER(7,2)), COMM (NUMBER(7,2)), DEPTNO (NUMBER(2))
MGR is the empno of the employee whom the employee reports to. DEPTNO is a foreign key.
QUERIES
1.
List all the employees who have at least one person reporting to them.
2.
List the employee details if and only if more than 10 employees are present in department no 10.
3.
List the name of the employees with their immediate higher authority.
4.
5.
List the employee details whose salary is greater than the lowest salary of an employee belonging to
deptno 20.
6.
List the details of the employee earning more than the highest paid manager.
7.
8.
9.
In which year did most people join the company? Display the year and the number of employees.
1.
SELECT DISTINCT(A.ENAME) FROM EMP A, EMP B WHERE A.EMPNO = B.MGR; or SELECT ENAME FROM
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM EMP GROUP BY DEPTNO HAVING
SELECT A.ENAME "EMPLOYEE", B.ENAME "REPORTS TO" FROM EMP A, EMP B WHERE A.MGR=B.EMPNO;
4.
SELECT * FROM EMP WHERE EMPNO IN ( SELECT EMPNO FROM EMP MINUS SELECT MGR FROM EMP);
5.
SELECT * FROM EMP WHERE SAL > ( SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO HAVING
DEPTNO=20);
6.
SELECT * FROM EMP WHERE SAL > ( SELECT MAX(SAL) FROM EMP GROUP BY JOB HAVING JOB =
'MANAGER' );
7.
8.
SELECT * FROM EMP WHERE (DEPTNO, HIREDATE) IN (SELECT DEPTNO, MAX(HIREDATE) FROM EMP
GROUP BY DEPTNO);
9.
table and DEPT table. EMPLOYEE table will contain 10 records pertaining to 10 employees with funny sounding names of
an imaginary organization and DEPT or Department table will contain 5 departments of that organization. Click here to
download the DDL/INSERT statements for this data if you want to practice the below SQLs in your personal computer
Contents of these tables are not same with Oracle emp and dept tables!!
What is the difference between inner and outer join? Explain with
example.
Inner Join
Inner join is the most common type of Join which is used to combine the rows from two tables and create a result set
containing only such records that are present in both the tables based on the joining condition (predicate).
Inner join returns rows when there is at least one match in both tables
If none of the record matches between two tables, then INNER JOIN will return a NULL set. Below is an example of
INNER JOIN and the resulting set.
Department Employee
HR
Inno
HR
Privy
Engineering Robo
Engineering Hash
Engineering Anno
Engineering Darl
Marketing Pete
Marketing Meme
Sales
Tomiti
Sales
Bhuti
Outer Join
Department Employee
HR
Inno
HR
Privy
Engineering Robo
Engineering Hash
Engineering Anno
Engineering Darl
Marketing
Marketing
Sales
Sales
Logistics
Pete
Meme
Tomiti
Bhuti
The (+) sign on the emp side of the predicate indicates that emp is the outer table here. The above SQL can be
alternatively written as below (will yield the same result as above):
ID NAME
4 Sales
Logistics
Next, suppose we want to see only those Departments where Average salary is greater than 80. Here the condition is
associated with a non-static aggregated information which is average of salary. We will need to use HAVING clause
here:
DEPARTMENT AVG_SAL
Engineering
90
As you see above, there is only one department (Engineering) where average salary of employees is greater than 80.
UNION
Self Join is often very useful to convert a hierarchical structure into a flat
structure
In our employee table example above, we have kept the manager ID of each employee in the same row as that of the
employee. This is an example of how a hierarchy (in this case employee-manager hierarchy) is stored in the RDBMS
table. Now, suppose if we need to print out the names of the manager of each employee right beside the employee, we
can use self join. See the example below:
EMPLOYEE MANAGER
Pete
Hash
Darl
Hash
Inno
Hash
Robo
Hash
Tomiti
Robo
Anno
Robo
Privy
Robo
Meme
Pete
Bhuti
Tomiti
Hash
The only reason we have performed a left outer join here (instead of INNER JOIN) is we have one employee in this table
without a manager (employee ID = 1). If we perform inner join, this employee will not show-up.
The column that is used in the row number generation logic is called sort key. Here sort key is name column. For this
technique to work, the sort key needs to be unique. We have chosen the column name because this column happened
to be unique in our Employee table. If it was not unique but some other collection of columns was, then we could have
used those columns as our sort key (by concatenating those columns to form a single sort key).
Also notice how the rows are sorted in the result set. We have done an explicit sorting on the row_num column, which
gives us all the row numbers in the sorted order. But notice that name column is also sorted (which is probably the reason
why this column is referred as sort-key). If you want to change the order of the sorting from ascending to descending, you
will need to change >= sign to <= in the query.
As I said before, this method is not very generic. This is why many databases already implement other methods to
achieve this. For example, in Oracle database, every SQL result set contains a hidden column called ROWNUM. We can
just explicitly select ROWNUM to get sequence numbers.
SELECT *
FROM EMP
WHERE ROWNUM <= 5;
In SQL Server,
SELECT name
FROM EMPLOYEE o
WHERE (SELECT count(*) FROM EMPLOYEE i WHERE i.name < o.name) < 5
name
Inno
Anno
Darl
Meme
Bhuti
I have taken name column in the above example since name is happened to be unique in this table. I could very well
take ID column as well.
In this example, if the chosen column was not distinct, we would have got more than 5 records returned in our output.
Do you have a better solution to this problem? If yes, post your solution in the comment.
name
Hash
Robo
Anno
Darl
Tomiti
Pete
Bhuti
Meme
Inno
Privy
Sal ROWNUM_BY_SAL
100 1
100 2
80 3
80 4
70 5
70 6
60 7
60 8
50 9
50 10
name
Hash
Robo
Anno
Darl
Tomiti
Pete
Bhuti
Meme
Inno
Privy
Sal RANK_BY_SAL
100 1
100 1
80 3
80 3
70 5
70 5
60 7
60 7
50 9
50 9
DENSE_RANK, like RANK, does not assign unique numbers, but it does assign contiguous numbers. Even though two
records tied for second place, there is a third-place record. See below:
name
Hash
Robo
Anno
Darl
Tomiti
Pete
Bhuti
Meme
Inno
Privy
Sal DENSE_RANK_BY_SAL
100 1
100 1
80 2
80 2
70 3
70 3
60 4
60 4
50 5
50 5
The data usage in any organization can be broadly divided as Operational and
Analytical.
Real time: If this data integration need is Real time then SAP Net weaver
PI will be used for this purpose.
Batch load: For batch loading of this data in periodical time intervals we use
SAP Data Services.
2.
Reporting / Analytics: Data from multiple time periods and multiple
divisions will be summarized and loaded into a data warehouse like SAP
HANA. Then reports will be generated on top of this data to give insights to
companies operations for a period of time. Examples of the reports are
Monthly sales report by Zip code. SAP dataservices is default tool for this
purpose.