70 Flashcards
Database Interview Question-1. What is “index” used for?
Index is a lookup table for database records in physical disk. Index makes record searching faster, it is similar to book index. Below figure explains the concept of index pictorially.
Database Interview Question-2. When to not use index?
Answer: Index should not be used for following cases:
· If number of records are small
· If records need to be updated/inserted/deleted on a daily basis
· If column value can be null very often
Database Interview Question-3. What is tempdb?
Answer: tempdb is temporary database which can be used by all users/processes to maintain temporary tables, etc. Content of tempdb is erased when database server is restarted.
Database Interview Question-4. How will you find out all the stored procedures present in a database?
Answer: SELECT object_name, object_type
FROM user_objects
WHERE object_type = ‘PROCEDURE’
Database Interview Question-5. What are the differences between TRUNCATE and DELETE?
Answer: TRUNCATE removes all rows from a table. The operation cannot be rolled back, also triggers are not executed for TRUNCATE
Database Interview Question-6. What is the difference between IN and EXISTS ?
Answer: EXISTS checks if any such record exists. e.g.
SELECT * FROM Orders o WHERE EXISTS (SELECT * FROM Products p where p.ProductNumber = o.ProductNumber)
IN is used to find presence of a record among multiple records. e.g.
SELECT * FROM Orders WHERE ProductNumber IN (1, 10, 100)
Database Interview Question-7. What database parameters can be tuned for performance?”
Answer: Buffer pool, Log level, etc.
Database Interview Question-8. What is bcp?
Answer: bcp in Sybase helps to copy table into a flat file and vice versa.
Database Interview Question-9. Suppose you are logged onto a database DB1. What query will you run in DB1 to retrieve data from the table table1 present in another database DB2?
Answer: We can use either select * from DB2..table1 or use DB2;select * from table1
Database Interview Question-10. What are main Open source databases?
Answer: PostgreSQL, MySQL, etc.
Database Interview Question-11. What is a View?
Answer: A view is the representation of a SQL statement that is stored in memory so that it can easily be re-used as example CREATE VIEW [MyProductList] AS SELECT ProductID,ProductName FROM Products WHERE Discontinued=No SELECT * FROM [MyProductList]
Database Interview Question-12. There is a table “Employee” having two columns – Emp_Name and Office_Locations. Write a query that will display all office locations and the number of employees in each of these locations.
Answer: Select Office_Locations, COUNT(Emp_Name) FROM Employee GROUP BY Office_Locations;
Database Interview Question-13. Write a query to get the employees, who are also managers
Answer: Select name from employees where id in (select manager from employees)
Or
Select e1.name from employees e1 inner join employees e2 ON e1.id = e2.manager
Database Interview Question-14. How to get name of database?
Answer: select db_name();
Database Interview Question-15. How to get database version?
Answer: select @@version
Database Interview Question-16. If table is truncated, can it be roll backed?
Answer: No
Database Interview Question-17. What is clustered index? How many Clustered indexes can you have on a table.
Answer: A clustered index physically sorts the records on the disk based on the index. Hence only one clustered index per table is feasible
Database Interview Question-18. What is “MINUS” used for?
Answer: MINUS returns only those results from first result set which are not present in second result set
Database Interview Question-19. Write a query to get all the employees who are managers
Answer: select distinct e.NAME as Employee, m.NAME as Manager from EMPLOYEE e inner join EMPLOYEE m on e.ID = m.MANAGER;
Database Interview Question-20. Write a query to get all the employees who do not have managers
Answer: Select ID, NAME from employee where MANAGER is NULL;
Database Interview Question-21. Suppose a table X has 3 duplicate rows and you want to delete 2 redundant rows ID Name Level 101 Jack 3 101 Jack 3 101 Jack 3
Answer: BEGIN; set rowcount 2; delete from X; END;
Database Interview Question-22. There is a table “Employee” having two columns – Emp_Name and Office_Locations. Write a query that will display all office locations having more than 100 employees.
Answer: select Office_Locations from (select Office_Locations, COUNT(Emp_name) as count1 from employee_table GROUP BY Office_Locations) HAVING count1>=100;
Database Interview Question-23. Can a table span multiple partitions?
Yes
Database Interview Question-24. What is foreign key?
Answer: Foreign key is a column or a combination of columns that is used to establish and enforce a link between two table
Database Interview Question-25. How to remove duplicates from a table?
SELECT DISTINCT * INTO NODUPLICATES FROM DUPLICATES;
DROP TABLE DUPLICATES;
RENAME TABLE NODUPLICATES TO DUPLICATES