You are on page 1of 12

Assignments

1. What is Projection, Selection and Joining. Ans. Projection identifies the entire row in a table,whereas selection identifies the column of a table.Joining is simply join of two tables by having atleast one key to be common. 2. List employee names from emp table Ans.Select First_name From employee 3. List all the details from dept table. Ans. Desc Department 4. List distinct designations from emp table Ans. Select distinct From Employee 5. List all the details from emp table so that each column name should give proper meaning. Ans.Desc Employee 6. *Explain Operator Precedence. Ans 7. List all the employees and their salaries. Comm component should be included with salary. Ans. Select First_name,Salary,Salary+Salary*(Commission_pct/100) as Increased_salary From employee 8. *Write a query whose output is Output = Salary + Commission If Sal= 10000 Commission = Null Ans.Select 9. List all the Details of employees and their column name should give proper meaning. Ans. Employee_id Number employee name First_name varchar2 first name Last_name varchar2 last name Email varchar2 email Phone_number varchar2 phone number Hire_date date date of joining Job_id varchar2 job unique id Salary Number salary Comminsion_pct Number commission percentage Manager_id Number manager id Department_id Number department id 10.List all the employees along with their job. Output should represent one field and the column name should give proper meaning. Ans. Select first_name|| ||job_id As Employee_job From employee 11.List Deptno and job from emp table eliminating duplicate rows.

Ans.Select distinct Department_id,job_id From employee 12.List out employee details along with interviewed date one week lesser than hiredate. Ans. Select last_name, to_date(hire_date,'dd-mm-yy')-7 as interview_date From employees 13. *What is the maximum width can a character field can hold in a database? 14. *What is the maximum precision can a number field can hold in a database? Is it possible to create number field without any precision. If so, what is the default value will it take? 15.List all the employees whose designation is MANAGER. Ans Select * From employee Where job_id=%MGR 16.List all the employees whose salary is greater than 1500. Ans.Select * From employee Where salary>1500 17. *List all the employees who joined in the year 1980,1981 and 1982. Ans.Select * From employee Where Hire_date.year IN(1980,1981,1982) 18. *List all the employees who belong to NEW YORK and DALLAS. Ans. Select * From employee Where 19.List all the employees a. whose name starts with A Select First_name From employee Where First_name like A% b. whose name contains A Select First_name From employee Where First_name like %A% c. whose name ends with A Select First_name From employee Where First_name like %A 20.List all the employee names in ascending and descending order. Ans.Select * From employee Order by First_name 21.List all the employees who belong to SALES department. Ans.Select * From employee Where job-id=SA%

22. *List all the employees who do not belong to BOSTON and CHICAGO.

Select * From employee Where 23.List the maximum salary in each department. Ans. Select department_id,max(salary) From employees group by department_id 24. *List the second lowest salary, employee name from each department. 25.List the total salary of each department. Ans. Select department_id,sum(salary) From employees group by department_id 26. *List the employees who are working under Mgr 27. *List the employees who joined in any year but not belongs to the month of March. 28. *List the details of the employees whose salaries more than the employee BLAKE. 29. *List the employees who are Senior to BLAKE working at CHICAGO & NEW YORK 30. List the employees whose jobs same as ALLEN Or SMITH 31.Find the details of highest paid employee. 32.List the most recently hired employee belongs to the Loc CHICAGO 33.List all the employees except 'MANAGER' & 'PRESIDENT' in asc order of salaries 34.List the employees whose empno not starting with digit 78 Ans. Select * From employee Where employee_id <>78% 35.List the employee name, job and Manager who are without Manager Ans.Select First_name,job_id,manager_id From employee Where manager_id IS NULL 36.Find out least 5 earners of the company in ascending order. Ans. 37.List those employees whose salary is odd value Ans.Select * From employees Where 38.List those employees whose salary is odd value 39.Produce the following output from EMP for all the employees( Case sensitive). EMPLOYEE ---------------SMITH(clerk) ALLEN(salesman) 40.List those managers who are getting less than his employees salary. 41.Print the details of all the employees who are sub-ordinates to BLAKE 42. *List the employees whose first 2 chars from hiredate = last 2 chars of

Salary. 1. Show the structure of the DEPARTMENTS table. Select all data from the DEPARTMENTS table. Ans. By the command desc,it will show description of table department and all these will be the following entries:Department_id, Number Department_name, varchar Manager_id, Number Location_id Number 2. Show the structure of the EMPLOYEES table. Create a query to display the last name, job code, hire date, and employee number for each employee, with employee number appearing first. Provide an alias STARTDATE for the HIRE_DATE column. Ans. Desc command will show the description of table employee,following are the entries Employee_id Number First_name varchar2 Last_name varchar2 Email varchar2 Phone_number varchar2 Hire_date date Job_id varchar2 Salary Number Comminsion_pct Number Manager_id Number Department_id Number Select last_name,job_id,employee_id,hire_date as start_date From employees 3. Display the last name concatenated with the job ID, separated by a comma and space, and name the column Employee and Title. Ans. select last_name ||','||' '||job_id As Employee from employees 4. Create a query to display all the data from the EMPLOYEES table. Separate each column by a comma. Name the column THE_OUTPUT. Ans. Select Employee_id||,||First_name||,||Last_name||,||Email||,||Phone_number||,|| Hire_date||,||Job_id||,||Salary||,||Comminsion_pct||,||Manager_id||,|| Department_id From Employees 5. Create a query to display the last name and salary of employees earning more than $12,000.

Ans. Select Last_name,salary From employees Where salary>12000 6. Create a query to display the employee last name and department number for employee number 176. Select last_name,department_id From employees Where employee_id=176 7. Display the employee last name, job ID, and start date of employees hired between February 20,1998, and May 1, 1998. Order the query in ascending order by start date. Ans. Select Last_name,job_id,hire_date From employee Where hire_date between 20-02-1998 and 01-05-1998 8. Display the last name and department number of all employees in departments 20 and 50 in alphabetical order by name. Ans. Select Last_name,department_id From employees Where Department_id between 20 and 50 Order by last_name *9. Display the last name and hire date of every employee who was hired in 1994. Ans. Select last_name,Hire_date From employee Where hire_date.year=1994 10. Display the last name and job title of all employees who do not have a manager. Ans. Select last_name,job_id From employees Where manager_id IS NULL 11. Display the last name, salary, and commission for all employees who earn commissions. Sort data in descending order of salary and commissions. Ans. Select last_name,salary,nvl(commission_pct,0) From employees where commission_pct>0 Order by salary,commission_pct DESC

12. Display the last names of all employees where the third letter of the name is an a. Ans. Select last_name From employees Where first_name like '__a%' 13. Display the last name of all employees who have an a and an e in their last name. Ans. Select last_name From employees Where last_name like '%a%e%' or last_name like '%e%a%' 14.** Display the last name, job, and salary for all employees whose job is sales representative (SA_REP) or stock Clerk (ST_CLERK) and whose salary is not equal to $2,500, $3,500, or $7,000. Ans. Select last_name,job_id,salary From employees Where job_id like 'SA_REP' and salary NOT IN(2500,3500,7000) or job_id like 'ST_CLERK'and salary NOT IN(2500,3500,7000)

15. Write a query to display the current date. Ans. Select current_date From dual 16. For each employee, display the employee number, last_name, salary, and salary increased by 15% and expressed as a whole number. Label the column New Salary. Ans. Select employee_id,last_name,salary,salary + salary*(15/100) as New_salary From employees 17. Modify above query to add a column that subtracts the old salary from the new salary. Label the column Increase. Ans. Select Salary,New_salary,New_salary-Salary as increase From employee 18. Write a query that displays the employees last names with the first letter capitalized and all other letters lowercase and the length of the name for all employees whose name starts with J, A, or M.Give each column an appropriate label. Sort the results by the employees last names. Ans. select INITCAP(first_name),length(first_name) as length_name from employees

where first_name like 'J%' or first_name like 'A%' or first_name like 'M%' 19. For each employee, display the employees last name, and calculate the number of months between today and the date the employee was hired. Label the column MONTHS_WORKED. Order your results by the number of months employed. Round the number of months up to the closest whole number. 20. Write a query that produces the following for each employee: <employee last name> earns <salary> monthly but wants <3 times salary>. Label the column Dream Salaries. Ans. select first_name,salary,3*salary as dream_salary from employees 21. Create a query to display the last name and salary for all employees. Format the salary to be 15 characters long, left-padded with $. Label the column SALARY. Ans. select last_name,Lpad(salary,15,'$') as new_salary from employees 22. *Display each employees last name, hire date, and salary review date, which is the first Monday after six months of service. Label the column REVIEW. Format the dates to appear in the format similar to Monday, the Thirty-First of July, 2000. Ans. 23. Create a query that displays the employees last names and commission amounts. If an employee does not earn commission, put No Commission. Label the column COMM. Ans. select last_name,salary*(nvl(commission_pct,0)/100) as commission_amount from employees 24. Using the DECODE function, write a query that displays the grade of all employees based on the value of the column JOB_ID, as per the following data: JOB AD_PRES ST_MAN IT_PROG SA_REP ST_CLERK None of the GRADE A B C D E above 0

24(i) Display the last name, hire date and day of the week on which the employee started. Label the column DAY. Order the results by the day of the week starting with Monday.

24(ii) Create a query that displays the employees last names and indicates the amounts of their annual salaries with asterisk. Each asterisk signifies a thousand dollars. Sort the data in descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES. ******************* 1) What is Single-Row Function? Ans. Single-row functions return a single result row for every row of a queried

table or view. These functions can appear in select lists, WHERE clauses, START WITH and CONNECT BY clauses, and HAVING clauses.
2) Single Row function will not appear in one of the clause below a. WHERE clauses. b. START WITH and CONNECT BY clauses. c. Numeric Clause. d. Having clause. Ans c 2) What are three basic Categories of Single Row Function? Ans. The different types of SINGLE ROW FUNCTIONS are: 1. NUMBER Functions 2. DATE Functions 3. CHARACTER Functions 4. CONVERSION Functions 5. GENERAL Functions 3) Give any two example of Single Row Function of each category? Ans. Round,trunc 4) The output of SYSDATE Functions in SQL represent includes timestamp? a. TRUE b. FALSE Ans.false 5) What is the main difference in function SUBSTR and INSTRT (expecting answer is only the difference Not definition) Ans

The Oracle INSTR function is similar to the SUBSTR, except instead of returning the sub string, Oracle INSTR returns the location of the string
6) What is the symbolic representation of CONCAT function? Ans.

CONCAT (string1, string2,)


7) What is LPAD, LTRIM function? Give a query example? Ans.
lpad( string1, padded_length, [ pad_string ] ) ltrim( string1, [ trim_string ] )

8) Write a simple query and show the output by using RPAD function?
rpad( string1, padded_length, [ pad_string ] )

9) Write a simple query and show the output by using RTRIM function? Rtrim(string1,[trim_string]) 10) What is the syntax of TRUNC function explains with one query example? Ans. trunc ( date, [ format ] ) 11) TRUNC function is not belongs to one of the following below : a. Number b. Character c. Date Ans character What is LENGTH function in SQL? Explain with one query example?

12)

SELECT LEN(column_name) FROM table_name

13) One of the character functions that return number as result identify from the below list : a. REPLACE b. NLSSORT c. LENGTH d. None of the above. Ans length 14) Explain the NVL function? Give one example?

The syntax for the NVL function is: NVL(String1,replace_with) string1 is the string to test for a null value. replace_with is the value returned if string1 is null.

15)

What is REPLACE function? Give one example?

Replace(string1, string_to_replace, [ replacement_string ])


string1 is the string to replace a sequence of characters with another set of characters. string_to_replace is the string that will be searched for in string1. replacement_string is optional. All occurrences of string_to_replace will be replaced with replacement_string in string1. If the replacement_string parameter is omitted, the replace function simply removes all occurrences of string_to_replace, and returns the resulting string

16) What is CURRENT_DATE? Whether the system will allow to change the default format of date? Give any one example?
The Oracle CURRENT_DATE function returns the current date in the session time zone, in a value in the Gregorian calendar of datatype DATE. The format in which the date is displayed depends on NLS_DATE_FORMAT parameter. The default setting of NLS_DATE_FORMAT is DD-MON-YY. This returns a 2-digit day, a three-character month abbreviation, and a 2-digit year.

17) What is SYSDATE? Whether the system will allow to change the default format of date? Give any one example?
In Oracle PL/SQL, SYSDATE is a pseudo column which always returns the operating system's current datetime value of DATE type. The format of the DATE output depends on the value of NLS_DATE_FORMAT initialization parameter.

1. The SYSDATE function requires no arguments. 2. You cannot use the SYSDATE function in the condition of a CHECK constraint.

18) Give any two Miscellaneous Single Row Functions Ans round,trunc 19) What is COALESCE and DECODE function explain with query example?

The COALESCE function in SQL returns the first non-NULL expression among its arguments. It is the same as the following CASE statement: SELECT CASE ("column_name") WHEN "expression 1 is not NULL" THEN "expression 1" WHEN "expression 2 is not NULL" THEN "expression 2" ... [ELSE "NULL"] END FROM "table_name"
The DECODE function is analogous to the "IF THEN ELSE" conditional statement. DECODE works with values, columns, and expressions of all data types. The DECODE function compares expression against each search value in order. If a match (equality) is found between the expression and the search argument then it returns the corresponding result. If there is no match, the default value is returned (if defined), else it returns NULL. In case of a type compatibility mismatch, Oracle internally does an implicit conversion (if possible) to return the results. Interestingly, Oracle considers two NULLs to be equivalent while working with the DECODE function.

20) Write a SELECT statement which will get the result 'elloworld' from the string 'HelloWorld' using Single Row Functions?

Ans. Substr(helloworld,2,10)

25) Write a query to find the list of employees whose age is greater than 30 or so given a date of Birth column. (Note : Age column should not be in table)? Clue : Query has to be written using Datetime Functions. 26)Provide the valid abbreviation in ORACLE for the below DATETIME function.

21) Give the SQL statements which return the absolute value of -33 with label as {Absolute value calculated by [your own name]} by using single Row Function? 22) Which four statements correctly describe functions that are available in SQL? (Choose four.) a. INSTR returns the numeric position of a named character. b. NVL2 returns the first non-null expression in the expression list. c. TRUNCATE rounds the column, expression, or value to n decimal places. d. DECODE translates an expression after comparing it to each search value. e. TRIM trims the heading or trailing characters (or both) from a character string. f. NVL compares two expressions and returns null if they are equal, or the first expression if they are not equal. g. NULLIF compares two expressions and returns null if they are equal, or the first expression if they are not equal. Ans. A,b,d,e 23) Which SQL statement returns a numeric value? a. SELECT ADD_MONTHS(MAX(hire_Date), 6) FROM EMP; b. SELECT ROUND(hire_date)FROM EMP; c. SELECT sysdate-hire_date FROM EMP; d. SELECT TO_NUMBER(hire_date + 7)FROM EMP; Ans d

Example : Year - YYYY Date part : Quarter Month mm Dayofyear day Day dd Week Weekday Hour hh Minute min Second ss Millisecond
*********************************************************************

You might also like