Вы находитесь на странице: 1из 12

Database Questions Q) DML insert, update, delete DDL create, alter, drop, truncate, rename.

e. DQL select DCL grant, revoke. TCL commit, rollback, savepoint. Q) Normalization Normalization is the process of simplifying the relationship between data elements in a record. Q) Normal forms (i) 1st normal form : - 1st N.F is achieved when all repeating groups are removed, and P.K should be defined. (ii) 2nd normal form : - Eliminate any non full dependence of data item on record keys. (iii) 3rd normal form : - Eliminate any transitive dependence of data items on P.Ks. Q) Diff Primary key and a Unique key? What is foreign key? A) Both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only. Foreign key constraint prevents any actions that would destroy link between tables with the corresponding data values. A foreign key in one table points to a primary key in another table. Foreign keys prevent actions that would leave rows with foreign key values when there are no primary keys with that value. The foreign key constraints are used to enforce referential integrity. CHECK constraint is used to limit the values that can be placed in a column. The check constraints are used to enforce domain integrity. NOT NULL constraint enforces that the column will not accept null values. The not null constraints are used to enforce domain integrity, as the check constraints. Q) Diff Delete & Truncate? A) Rollback is possible after DELETE but TRUNCATE remove the table permanently and cant rollback. Truncate will remove the data permanently we cannot rollback the deleted data. Dropping : (Table structure + Data are deleted), Invalidates the dependent objects, Drops the indexes Truncating : (Data alone deleted), Performs an automatic commit, Faster than delete Delete : (Data alone deleted), Doesnt perform automatic commit Q) Diff Varchar and Varchar2? A) The difference between Varchar and Varchar2 is both are variable length but only 2000 bytes of character of data can be store in varchar where as 4000 bytes of character of data can be store in varchar2. Q) Diff LONG & LONG RAW? A) You use the LONG datatype to store variable-length character strings. The LONG datatype is like the VARCHAR2 datatype, except that the maximum length of a LONG value is 32760 bytes. You use the LONG RAW datatype to store binary data (or) byte strings. LONG RAW data is like LONG data, except that LONG RAW data is not interpreted by PL/SQL. The maximum length of a LONG RAW value is 32760

bytes. Q) Diff Function & Procedure Function is a self contained program segment, function will return a value but procedure not. Procedure is sub program will perform some specific actions. Q) How to find out duplicate rows & delete duplicate rows in a table? A) MPID EMPNAME EMPSSN ----- ---------- ----------1 Jack 555-55-5555 2 Mike 555-58-5555 3 Jack 555-55-5555 4 Mike 555-58-5555 SQL> select count (empssn), empssn from employee group by empssn having count (empssn) > 1; COUNT (EMPSSN) EMPSSN ------------- ----------2 555-55-5555 2 555-58-5555 SQL> delete from employee where (empid, empssn) not in (select min (empid), empssn from employee group by empssn); Q) Select the nth highest rank from the table? A) Select * from tab t1 where 2=(select count (distinct (t2.sal)) from tab t2 where t1.sal<=t2.sal) Q) a) Emp table where fields empName, empId, address b) Salary table where fields EmpId, month, Amount these 2 tables he wants EmpId, empName and salary for month November? A) Select emp.empId, empName, Amount from emp, salary where emp.empId=salary.empId and month=November; Q) Oracle/PLSQL: Synonyms? A) A synonym is an alternative name for objects such as tables, views, sequences, stored procedures, and other database objects Syntax: Create [or replace] [public] synonym [schema.] synonym_name for [schema.] object_name; or replace -- allows you to recreate the synonym (if it already exists) without having to issue a DROP synonym command. Public -- means that the synonym is a public synonym and is accessible to all users. Schema -- is the appropriate schema. If this phrase is omitted, Oracle assumes that you are referring to your own schema. object_name -- is the name of the object for which you are creating the synonym. It can be one of the following: Table Package View materialized view sequence java class schema object stored procedure user-defined object Function Synonym example: Create public synonym suppliers for app. suppliers; Example demonstrates how to create a synonym called suppliers. Now, users of other schemas can reference the table called suppliers without having to prefix the table name with the schema named app. For example: Select * from suppliers;

If this synonym already existed and you wanted to redefine it, you could always use the or replace phrase as follows: Create or replace public synonym suppliers for app. suppliers; Dropping a synonym It is also possible to drop a synonym. drop [public] synonym [schema .] synonym_name [force]; public -- phrase allows you to drop a public synonym. If you have specified public, then you don't specify a schema. Force -- phrase will force Oracle to drop the synonym even if it has dependencies. It is probably not a good idea to use the force phrase as it can cause invalidation of Oracle objects. example: Drop public synonym suppliers; This drop statement would drop the synonym called suppliers that we defined earlier. Q) What is an alias and how does it differ from a synonym? A) An alias is an alternative to a synonym, designed for a distributed environment to avoid having to use the location qualifier of a table or view. The alias is not dropped when the table is dropped. Q) What are joins? Inner join & outer join? A) By using joins, you can retrieve data from two or more tables based on logical relationships between the tables Inner Join: - returns all rows from both tables where there is a match Outer Join: - outer join includes rows from tables when there are no matching values in the tables. LEFT JOIN or LEFT OUTER JOIN The result set of a left outer join includes all the rows from the left table specified in the LEFT OUTER clause, not just the ones in which the joined columns match. When a row in the left table has no matching rows in the right table, the associated result set row contains null values for all select list columns coming from the right table. RIGHT JOIN or RIGHT OUTER JOIN. A right outer join is the reverse of a left outer join. All rows from the right table are returned. Null values are returned for the left table any time a right table row has no matching row in the left table. FULL JOIN or FULL OUTER JOIN. A full outer join returns all rows in both the left and right tables. Any time a row has no match in the other table, the select list columns from the other table contain null values. When there is a match between the tables, the entire result set row contains data values from the base tables. Q. Diff join and a Union? A) A join selects columns from 2 or more tables. A union selects rows. when using the UNION command all selected columns need to be of the same data type. The UNION command eliminate duplicate values. Q. Union & Union All? A) The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values. It cannot eliminate duplicate values. > SELECT E_Name FROM Employees_Norway UNION ALL SELECT E_Name FROM Employees_USA

Q) Is the foreign key is unique in the primary table? A)Not necessary Q) Table mentioned below named employee ID NAME MID 1 CEO Null 2 VP CEO 3 Director VP Asked to write a query to obtain the following output CEO Null VP CEO Director VP A) SQL> Select a.name, b.name from employee a, employee b where a.mid=b.id(+). Q) Explain a scenario when you dont go for normalization? A) If we r sure that there wont be much data redundancy then dont go for normalization. Q) What is Referential integrity? A) R.I refers to the consistency that must be maintained between primary and foreign keys, i.e. every foreign key value must have a corresponding primary key value. Q) What is Normalization? Explain in details? A) Normalization is the process of simplifying the relation ship between data elements in a record. 1st N.F -- the big table is broken into many small tables, such that each table has a primary key. 2nd N.F Removes partial dependency. Ie The columns in a table which is not completely dependant on the primary key are taken to a separate table 3rd N.F Removes Transitive dependency. Ie If X is the primary key in a table. Y & Z are columns in the same table. Suppose Z depends only on Y and Y depends on X. Then Z does not depend directly on primary key. So remove Z from the table to a look up table. Q) What techniques are used to retrieve data from more than one table in a single SQL statement? A) Joins, unions and nested selects are used to retrieve data. Q) What is a view? Why use it? A) A view is a virtual table made up of data from base tables and other views, but not stored separately. Q) SELECT statement syntax? A) SELECT [ DISTINCT | ALL ] column_expression1, column_expression2, .... [ FROM from_clause ] [ WHERE where_expression ] [ GROUP BY expression1, expression2, .... ] [ HAVING having_expression ] [ ORDER BY order_column_expr1, order_column_expr2, .... ] column_expression ::= expression [ AS ] [ column_alias ] from_clause ::= select_table1, select_table2, ... from_clause ::= select_table1 LEFT [OUTER] JOIN select_table2 ON expr ... from_clause ::= select_table1 RIGHT [OUTER] JOIN select_table2 ON expr ... from_clause ::= select_table1 [INNER] JOIN select_table2 ... select_table ::= table_name [ AS ] [ table_alias ] select_table ::= ( sub_select_statement ) [ AS ] [ table_alias ] order_column_expr ::= expression [ ASC | DESC ] Q) DISTINCT clause? A) The DISTINCT clause allows you to remove duplicates from the result set. > SELECT DISTINCT city FROM supplier;

Q) COUNT function? A) The COUNT function returns the number of rows in a query > SELECT COUNT (*) as "No of emps" FROM employees WHERE salary > 25000; Q) Diff HAVING CLAUSE & WHERE CLAUSE? A) Having Clause is basically used only with the GROUP BY function in a query. WHERE Clause is applied to each row before they are part of the GROUP BY function in a query. Q) Diff GROUP BY & ORDER BY? A) Group by controls the presentation of the rows, order by controls the presentation of the columns for the results of the SELECT statement. > SELECT "col_nam1", SUM("col_nam2") FROM "tab_name" GROUP BY "col_nam1" > SELECT "col_nam" FROM "tab_nam" [WHERE "condition"] ORDER BY "col_nam" [ASC, DESC] Q) What keyword does an SQL SELECT statement use for a string search? A) The LIKE keyword allows for string searches. The % sign is used as a wildcard. Q) What is a NULL value? What are the pros and cons of using NULLS? A) NULL value takes up one byte of storage and indicates that a value is not present as opposed to a space or zero value. A NULL in a column means no entry has been made in that column. A data value for the column is "unknown" or "not available." Q) Index? Types of indexes? A) Locate rows more quickly and efficiently. It is possible to create an index on one (or) more columns of a table, and each index is given a name. The users cannot see the indexes, they are just used to speed up queries. Unique Index : A unique index means that two rows cannot have the same index value. >CREATE UNIQUE INDEX index_name ON table_name (column_name) When the UNIQUE keyword is omitted, duplicate values are allowed. If you want to index the values in a column in descending order, you can add the reserved word DESC after the column name: >CREATE INDEX PersonIndex ON Person (LastName DESC) If you want to index more than one column you can list the column names within the parentheses. >CREATE INDEX PersonIndex ON Person (LastName, FirstName) Q) Diff subqueries & Correlated subqueries? A)subqueries are self-contained. None of them have used a reference from outside the subquery. correlated subquery cannot be evaluated as an independent query, but can reference columns in a table listed in the from list of the outer query. Q) Predicates IN, ANY, ALL, EXISTS ? A) Sbquery can return a subset of zero to n values. According to the conditions which one wants to express, one can use the predicates IN, ANY, ALL or EXISTS. IN The comparison operator is the equality and the logical operation between values is OR. ANY Allows to check if at least a value of the list satisfies condition. ALL Allows to check if condition is realized for all the values of the list. EXISTS If the subquery returns a result, the value returned is True otherwise the value returned is False. Q) What are some sql Aggregates and other Built-in functions? A) AVG, SUM, MIN, MAX, COUNT and DISTINCT.

SQL Queries
Hi In Real Time Database also Very Important in Manual Testers,Ihave Mention Some database Concepts plz Gothrough this, Create the following Tables: LOCATION Location_ID Regional_Group 122 NEW YORK 123 DALLAS 124 CHICAGO 167 BOSTON DEPARTMENT Department_ID Name 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS JOB Job_ID Function 667 CLERK 668 STAFF 669 ANALYST 670 SALESPERSON 671 MANAGER 672 PRESIDENT
EMPL OYEE _ID 7369 7499 7505 7506 LAST_N AME SMITH ALLEN DOYLE DENNIS FIRST _NAM E JOHN KEVIN JEAN LYNN

Location_ID 122 124 123 167

EMPLOYEE MID DLE _NA ME Q J K S JOB_I D 667 670 671 671 MANA GER_I D 7902 7698 7839 7839 HIRED ATE 17DEC84 20FEB85 04APR85 15MAYSALA RY 800 1600 2850 2750 COM M NULL 300 NULL NULL DEPA RTME NT_ID 20 30 30 30

7507 7521

BAKER WARK

LESLI E CYNT HIA

D D

671 670

7839 7698

85 10JUN85 22FEB85

2200 1250

NULL 500

40 30

Queries based on the above tables: Simple Queries: 1. List all the employee details 2. List all the department details 3. List all job details 4. List all the locations 5. List out first name,last name,salary, commission for all employees 6. List out employee_id,last name,department id for all employees and rename employee id as ID of the employee, last name as Name of the employee, department id as department ID 7. List out the employees anuual salary with their names only. Where Conditions: 8. List the details about SMITH 9. List out the employees who are working in department 20 10. List out the employees who are earning salary between 3000 and 4500 11. List out the employees who are working in department 10 or 20 12. Find out the employees who are not working in department 10 or 30 13. List out the employees whose name starts with S 14. List out the employees whose name start with S and end with H 15. List out the employees whose name length is 4 and start with S 16. List out the employees who are working in department 10 and draw the salaries more than 3500 17. list out the employees who are not receiving commission. Order By Clause: 18. List out the employee id, last name in ascending order based on the employee id. 19. List out the employee id, name in descending order based on salary column 20. list out the employee details according to their last_name in ascending order and salaries in descending order 21. list out the employee details according to their last_name in ascending order and then on department_id in descending order. Group By & Having Clause: 22. How many employees who are working in different departments wise in the organization 23. List out the department wise maximum salary, minimum salary, average salary of the employees 24. List out the job wise maximum salary, minimum salary, average salaries of the employees. 25. List out the no.of employees joined in every month in ascending order.

26. List out the no.of employees for each month and year, in the ascending order based on the year, month. 27. List out the department id having atleast four employees. 28. How many employees in January month. 29. How many employees who are joined in January or September month. 30. How many employees who are joined in 1985. 31. How many employees joined each month in 1985. 32. How many employees who are joined in March 1985. 33. Which is the department id, having greater than or equal to 3 employees joined in April 1985. Sub-Queries 34. Display the employee who got the maximum salary. 35. Display the employees who are working in Sales department 36. Display the employees who are working as Clerk. 37. Display the employees who are working in New York 38. Find out no.of employees working in Sales department. 39. Update the employees salaries, who are working as Clerk on the basis of 10%. 40. Delete the employees who are working in accounting department. 41. Display the second highest salary drawing employee details. 42. Display the Nth highest salary drawing employee details Sub-Query operators: (ALL,ANY,SOME,EXISTS) 43. List out the employees who earn more than every employee in department 30. 44. List out the employees who earn more than the lowest salary in department 30. 45. Find out whose department has not employees. 46. Find out which department does not have any employees. Co-Related Sub Queries: 47.Find out the employees who earn greater than the average salary for their department. Joins Simple join 48.List our employees with their department names 49.Display employees with their designations (jobs) 50.Display the employees with their department name and regional groups. 51.How many employees who are working in different departments and display with department name. 52.How many employees who are working in sales department. 53.Which is the department having greater than or equal to 5 employees and display the department names in ascending order. 54.How many jobs in the organization with designations. 55.How many employees working in New York. Non Equi Join: 56.Display employee details with salary grades. 57.List out the no. of employees on grade wise. 58.Display the employ salary grades and no. of employees between 2000 to 5000 range of salary

Self Join: 59.Display the employee details with their manager names. 60.Display the employee details who earn more than their managers salaries. 61.Show the no. of employees working under every manager. Outer Join: 61.Display employee details with all departments. 62.Display all employees in sales or operation departments. Set Operators: 63.List out the distinct jobs in Sales and Accounting Departments. 64.List out the ALL jobs in Sales and Accounting Departments. 65.List out the common jobs in Research and Accounting Departments in ascending order. Answers 1. 2. 3. 4. 5. 6. SQL > Select * from employee; SQL > Select * from department; SQL > Select * from job; SQL > Select * from loc; SQL > Select first_name, last_name, salary, commission from employee; SQL > Select employee_id id of the employee, last_name name", department id as department id from employee; 7. SQL > Select last_name, salary*12 annual salary from employee 8. SQL > Select * from employee where last_name=SMITH; 9. SQL > Select * from employee where department_id=20 10. SQL > Select * from employee where salary between 3000 and 4500 11. SQL > Select * from employee where department_id in (20,30) 12. SQL > Select last_name, salary, commission, department_id from employee where department_id not in (10,30) 13. SQL > Select * from employee where last_name like S% 14. SQL > Select * from employee where last_name like S%H 15. SQL > Select * from employee where last_name like S___ 16. SQL > Select * from employee where department_id=10 and salary>3500 17. SQL > Select * from employee where commission is Null 18. SQL > Select employee_id, last_name from employee order by employee_id 19. SQL > Select employee_id, last_name, salary from employee order by salary desc 20. SQL > Select employee_id, last_name, salary from employee order by last_name, salary desc 21. SQL > Select employee_id, last_name, salary from employee order by last_name, department_id desc

22. SQL > Select department_id, count(*), from employee group by department_id 23. SQL > Select department_id, count(*), max(salary), min(salary), avg(salary) from employee group by department_id 24. SQL > Select job_id, count(*), max(salary), min(salary), avg(salary) from employee group by job_id 25. SQL > Select to_char(hire_date,month)month, count(*) from employee group by to_char(hire_date,month) order by month 26. SQL > Select to_char(hire_date,yyyy) Year, to_char(hire_date,mon) Month, count(*) No. of employees from employee group by to_char(hire_date,yyyy), to_char(hire_date,mon) 27. SQL > Select department_id, count(*) from employee group by department_id having count(*)>=4 28. SQL > Select to_char(hire_date,mon) month, count(*) from employee group by to_char(hire_date,mon) having to_char(hire_date,mon)=jan 29. SQL > Select to_char(hire_date,mon) month, count(*) from employee group by to_char(hire_date,mon) having to_char(hire_date,mon) in (jan,sep) 30. SQL > Select to_char(hire_date,yyyy) Year, count(*) from employee group by to_char(hire_date,yyyy) having to_char(hire_date,yyyy)=1985 31. SQL > Select to_char(hire_date,yyyy)Year, to_char(hire_date,mon) Month, count(*) No. of employees from employee where to_char(hire_date,yyyy)=1985 group by to_char(hire_date,yyyy),to_char(hire_date,mon) 32. SQL > Select to_char(hire_date,yyyy)Year, to_char(hire_date,mon) Month, count(*) No. of employees from employee where to_char(hire_date,yyyy)=1985 and to_char(hire_date,mon)=mar group by to_char(hire_date,yyyy),to_char(hire_date,mon) 33. SQL > Select department_id, count(*) No. of employees from employee where to_char(hire_date,yyyy)=1985 and to_char(hire_date,mon)=apr group by to_char(hire_date,yyyy), to_char(hire_date,mon), department_id having count(*)>=3 34. SQL > Select * from employee where salary=(select max(salary) from employee) 35. SQL > Select * from employee where department_id IN (select department_id from department where name=SALES) 36. SQL > Select * from employee where job_id in (select job_id from job where function=CLERK 37. SQL > Select * from employee where department_id=(select department_id from department where location_id=(select location_id from location where regional_group=New York)) 38. SQL > Select * from employee where department_id=(select department_id from department where name=SALES group by department_id) 39. SQL > Update employee set salary=salary*10/100 wehre job_id=(select job_id from job where function=CLERK) 40. SQL > delete from employee where department_id=(select department_id from department where name=ACCOUNTING)

41. SQL > Select * from employee where salary=(select max(salary) from employee where salary <(select max(salary) from employee)) 42. SQL > Select distinct e.salary from employee where & no-1=(select count(distinct salary) from employee where sal>e.salary) 43. SQL > Select * from employee where salary > all (Select salary from employee where department_id=30) 44. SQL > Select * from employee where salary > any (Select salary from employee where department_id=30) 45. SQL > Select employee_id, last_name, department_id from employee e where not exists (select department_id from department d where d.department_id=e.department_id) 46. SQL > Select name from department d where not exists (select last_name from employee e where d.department_id=e.department_id) 47. SQL > Select employee_id, last_name, salary, department_id from employee e where salary > (select avg(salary) from employee where department_id=e.department_id) 48. SQL > Select employee_id, last_name, name from employee e, department d where e.department_id=d.department_id 49. SQL > Select employee_id, last_name, function from employee e, job j where e.job_id=j.job_id 50. SQL > Select employee_id, last_name, name, regional_group from employee e, department d, location l where e.department_id=d.department_id and d.location_id=l.location_id 51. SQL > Select name, count(*) from employee e, department d where d.department_id=e.department_id group by name 52. SQL > Select name, count(*) from employee e, department d where d.department_id=e.department_id group by name having name=SALES 53. SQL > Select name, count(*) from employee e, department d where d.department_id=e.department_id group by name having count (*)>=5 order by name 54. SQL > Select function, count(*) from employee e, job j where j.job_id=e.job_id group by function 55. SQL > Select regional_group, count(*) from employee e, department d, location l where e.department_id=d.department_id and d.location_id=l.location_id and regional_group=NEW YORK group by regional_group 56. SQL > Select employee_id, last_name, grade_id from employee e, salary_grade s where salary between lower_bound and upper_bound order by last_name 57. SQL > Select grade_id, count(*) from employee e, salary_grade s where salary between lower_bound and upper_bound group by grade_id order by grade_id desc 58. SQL > Select grade_id, count(*) from employee e, salary_grade s where salary between lower_bound and upper_bound and lower_bound>=2000 and lower_bound<=5000 group by grade_id order by grade_id desc 59. SQL > Select e.last_name emp_name, m.last_name, mgr_name from employee e, employee m where e.manager_id=m.employee_id

60. SQL > Select e.last_name emp_name, e.salary emp_salary, m.last_name, mgr_name, m.salary mgr_salary from employee e, employee m where e.manager_id=m.employee_id and m.salary<e.salary 61. SQL > Select m.manager_id, count(*) from employee e, employee m where e.employee_id=m.manager_id group by m.manager_id 62. SQL > Select last_name, d.department_id, d.name from employee e, department d where e.department_id(+)=d.department_id 63. SQL > Select last_name, d.department_id, d.name from employee e, department d where e.department_id(+)=d.department_id and d.department_idin (select department_id from department where name IN (SALES,OPERATIONS)) 64. SQL > Select function from job where job_id in (Select job_id from employee where department_id=(select department_id from department where name=SALES)) union Select function from job where job_id in (Select job_id from employee where department_id=(select department_id from department where name=ACCOUNTING)) 65. SQL > Select function from job where job_id in (Select job_id from employee where department_id=(select department_id from department where name=SALES)) union all Select function from job where job_id in (Select job_id from employee where department_id=(select department_id from department where name=ACCOUNTING)) 66. SQL > Select function from job where job_id in (Select job_id from employee where department_id=(select department_id from department where name=RESEARCH)) intersect Select function from job where job_id in (Select job_id from employee where department_id=(select department_id from department where name=ACCOUNTING)) order by function

Вам также может понравиться