12 Database SQL Index Interview Questions and Answers For 2 To 5 Years Experienced
12 Database SQL Index Interview Questions and Answers For 2 To 5 Years Experienced
Since many programmers just have shallow understating of index, they began to show gaps when
they face index based question on SQL Job interviews. I have taken many interviews and I am
surprise that many programmer doesn't even know what is index seek and index scan and which
one is faster? They are not even able to tell whether order of columns matter in a composite
index or not etc?
These are very common question and if you have work in SQL or database, you might have
faced those situations already. In this article, I have compiled such question to bring them
together and give them a nice overview. To keep the article short and simple, I haven't gone into
too much detail but I have pointed resource to learn more if you are curious about it.
Then it can use index-scan if you have a clustered index on Employee e.g. on EmployeeId. It's
generally faster than table scan but slower than index seek.
You can even see the actual Execution Plan in Microsoft SQL Server Management Studio by
pressing Ctrl + A and then running the queries as shown below:
In a table scan, SQL query engine, searches all records to find the matching rows, while in
index-scan it searches through index pages. If you don't have index in your table then table scan
is used, but if you have clustered index then it is used even when you don't specify any condition
in WHERE clause.
For example, select * from Books will use table scan if there is clustered index in table but will
use index-scan if there is a clustered index in the table.
On the other hand, non-clustered index create an alternative index tree to facilitate faster retrieval
of data. Since Clustered index define physical ordering, there can only be one clustered index in
a table but you can have as many non-clustered index as you want.
Here is a nice diagram which clearly shows the difference between clustered and non-clustered
or secondary indices in SQL:
9. I have a table which has clustered index on column 1, can I create another clustered
index on column 2?
No, you can only have one clustered index per table, so you cannot create second one, it's not
allowed.
This index is based upon three column col1, col2, and col3.
This query will use your composite index but following query will not use it becuase the
mandatory column firstname is not available:
Index data structure also take space in your database, so if you have multiple index tree then
obviously more space will be needed.
That's all about some of the frequently asked SQL and database interview questions based
upon Index. As I said, it's very important for every programmer working with database and SQL
to have a good understanding of index because it directly affects the performance of your
application. A person with good knowledge of index not only write faster SQL queries but also
quick to diagnose any performance issue with their SQL queries.
We can't depend upon Database administrator or DBAs for everything, hence I advice every
application developer to learn more about how index working their respective database and fill
the gaps in their knowledge. You can also checkout following resources to improve you
understanding of indexes in major databases like Oracle, MySQL, and Microsoft SQL Server.
Further Reading
Luke Index book
Thanks for reading this article so far. If you like these index based SQL interview questions then
please share with your friends and colleagues. If you have any question or feedback then please
drop a note.