Database / SQL
1. SQL to find 2nd highest salary in the Employee table.
One of the ways is to use correlated subquery to find the Nth highest salary as shown below. SELECT name, salary FROM employee emp1 WHERE N - 1 = ( SELECT COUNT ( DISTINCT salary) FROM Employee emp2 WHERE emp2.salary...
2. Difference between RANK and DENSE_RANK functions in SQL.
RANK generates the ranking within your ordered partition. Ties are assigned the same rank, with the next ranking(s) skipped....
3. Default behavior of NULL on ORDER BY Clause in SQL.
NULLS are ordered at LAST for ascending order, and NULLS are ordered FIRST in case of descending order.
4. How do I order NULLS first while performing ascending order in SQL?
Extend the order by clause to include an optional NULLS FIRST or NULLS LAST clauses as shown below. SELECT * FROM Employee ORDER BY dept_id ASC NULLS FIRST ; SELECT * FROM Employee ORDER BY dept_id DESC NULLS LAST ;
5. What is ROWID in SQL?
ROWID is the physical location of a row . It is the fastest way of locating a row, faster even than a primary key lookup....
6. Explain ROWNUM in Oracle.
For each row returned by a query, the ROWNUM pseudocolumn returns a number indicating the order in which Oracle selects the row from a table or set of joined rows. The first row selected has a ROWNUM of 1, the...
7. How the ROWNUM to be used together with the ORDER BY clause in SQL?
If an ORDER BY clause follows ROWNUM in the same query, then the rows will be reordered by the ORDER BY clause. The results can vary depending on the way the rows are accessed....
8. What is Correlated subquery in SQL?
In a SQL database query, a correlated subquery (also known as a synchronized subquery) is a subquery (a query nested inside another query) that uses values from the outer query. Because the subquery is evaluated once for each row processed...
9. What is cardinality?
The ratio of distinct values to the number of table rows. A column with only two distinct values in a million-row table would have low cardinality.
10. Explain bind variable in SQL.
A placeholder in a SQL statement that must be replaced with a valid value or value address for the statement to execute successfully. By using bind variables, you can write a SQL statement that accepts inputs or parameters at run...
11. What is B-tree index?
An index organized like an upside-down tree. A B-tree index has two types of blocks: branch blocks for searching and leaf blocks that store values....
12. What is Clustered Index in SQL?
The clustered index is used to reorder the physical order of the table and search based on the key values. A table can have only one clustered index.
13. What is NonClustered Index in SQL?
NonClustered Index does not alter the physical order of the table and maintains logical order of data. A table can have 999 non-clustered indexes.
14. Explain index in database.
Indexes are schema objects that are logically and physically independent of the data in the objects with which they are associated. Thus, an index can be dropped or created without physically affecting the table for the index.
15. Explain composite index in database.
A composite index, also called a concatenated index, is an index on multiple columns in a table. Columns in a composite index should appear in the order that makes the most sense for the queries that will retrieve data and...
16. What is self join In SQL?
Self join is often very useful to convert a hierarchical structure to a flat structure. It is used to join a table to itself as like if that is the second table.
17. Explain ACID properties of database transactions.
ACID is a set of properties that you would like to apply when modifying a database. Atomicity, Consistency, Isolation, Durability....
18. Difference between a super key and a candidate key.
Candidate key is a super key from which you cannot remove any fields.
19. What is super key in database?
A super key is any combination of columns that uniquely identifies a row in a table.
20. What is the equivalent of GO statement in MySQL?
There is NO GO statement in MySQL. Semicolon (;) at the end of each line works as GO statement in MySQL.
21. Different types of JOIN in SQL.
INNER JOIN, LEFT JOIN, RIGHT JOIN, OUTER JOIN, LEFT JOIN EXCLUDING INNER JOIN, RIGHT JOIN EXCLUDING INNER JOIN, OUTER JOIN EXCLUDING INNER JOIN.
22. What is a cross join in SQL?
The SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table , assuming no 'where' clause is used along with CROSS JOIN. This...
23. Explain SQL SELF JOIN.
The SQL SELF JOIN is used to join a table to itself as if the table were two tables, by renaming at least one table with alias in the SQL statement.
24. Difference between primary key and unique key.
Primary key doesn't allow null while unique key allow null.
25. Difference between view and materialized view in SQL.
Views are virtual and the query is executed every time it is accessed. The upside of a view is that it will always return the latest data to you....
26. How do I find number of queries issued per hour in MySQL?
Using the show global status command we can determine the number of selects, updates since the last database restart. To find per day run the command now and after 24 hours and subtract the last count with the latest....
27. Disadvantages of SQL indexing.
Indexes may slow down inserts, deletes and updates due to locking and also costs disk space.
28. Advantages of using SQL Index.
Faster query execution, quick data retrieval, facilitates sorting and thus eliminate post fetch sorting strategy, Unique indexes uniquely identifies records in the database.
29. Types of SQL index.
There are 2 main types. Clustered indexes define the physical sorting of a database table rows in the storage media....
30. Can a view have same name as table under same tablespace?
No. A view and table cannot have same name.
31. What is the simplest way to find the second largest value using SQL?
To find the second largest, Use the max aggregate function to find the largest and filter the largest value and find the maximum value again that yields the second largest. SELECT MAX ( column ) FROM table WHERE column <...
32. Hi, I am Vinay, My question is how to create Empid (ex:Empid: TCS-456) and to save in Mysql BD, if next, I enter other then it should be incremented like Empid: TCS-456/7/8/9..etc.
Hi there, my suggestion would be using a DB sequence (or a separate table for the sequence in MYSQL DB) to generate a unique number (for example,456) and have a Database trigger (before-insert) to prefix "TCS-". This way we will...
33. What is the difference between Truncate & Delete?
DELETE is a DML statement that deletes complete rows from the table and produces the count of deleted rows. It also allows us to filter and delete any specific records using the WHERE clause from the table....
34. How do you find the second highest salary from a table?
Option 1 : WITH RANK_SALARY AS ( SELECT * , DENSE_RANK() OVER ( ORDER BY Salary Desc ) AS SALARY_RANK FROM Employees ) SELECT Name, Salary FROM RANK_SALARY WHERE SALARY_RANK = 2 Option 2 : SELECT Salary FROM Employees ORDER...
35. Explain different types of SQL joins.
There are different types of joins available in SQL: INNER JOIN : returns rows when there is a match in both tables. LEFT JOIN : returns all rows from the left table, even if there are no matches in the...
36. Different subsets of SQL.
SQL queries can be categories into 4 main Categories: DDL (Data Definition Language). As the name suggests, these types of queries are used to define the structure of data....
37. What are SQL constraints?
SQL Constraints are the guidelines used to create restrictions to be implemented on the contents of the table or database so that the records should obey these rules in order to be placed in that table. Some of the constraints...
38. SQL to find the 3rd highest salary.
You can get the third-highest salary by using limit, by using TOP keyword and sub-query. TOP works with MS SQL server and LIMIT works with MySQL....
39. How to best display in Terminal a MySQL SELECT returning too many fields?
Terminate the query with \G in place of ;. For example: SELECT * FROM sometable \ G
40. Write a SQL query to display employee name, and salary where the salary is highest for each manager.
select e1.emp_name, e1.mgr_name, e1.emp_salary from Employee e1, ( select mgr_name, max (emp_salary) empMaxSalary from Employee group by mgr_name ) e2 where e1.mgr_name = e2.mgr_name and e1.emp_salary = e2.empMaxSalary