SQL Interview Questions with Answers Part-1

SQL Interview Questions with Answers Part-1

Introduction

SQL is one of those skills that keeps showing up in job interviews, whether you’re applying for data analyst, developer, or even business intelligence roles. Why? Because almost every company today works with data, and SQL is the language to talk to that data.

WhatsApp Group Join Now
Telegram Group Join Now

If you’re preparing for interviews, mastering SQL basics is your first step. In this article, we’ll break down common SQL interview questions with beginner-friendly answers so even non-tech folks can get it.


What is SQL?

SQL stands for Structured Query Language. Simply put, it’s a way to communicate with databases. Imagine a database as a big digital cupboard filled with tables of information. You can ask questions like these using SQL:

  • “Show me all of the new hires for this year.”
  • “Update the salary of John.”
  • “Delete inactive accounts.”

That’s SQL in action!


SQL vs NoSQL Databases

Think of SQL databases like an Excel sheet — rows and columns, neat and structured. Perfect for financial records, HR data, or any place where data fits a strict format.

NoSQL databases are more flexible, like a messy diary where each page looks different. They’re great for handling social media feeds, IoT data, or unstructured information like JSON.

In short:

  • SQL = Structured, reliable, great for transactions.
  • NoSQL = Flexible, scalable, great for large or unstructured data.

Types of SQL Commands

SQL commands are grouped into 4 main categories:

  1. Data Definition Language, or DDL, is concerned with structure.
  2. Data Manipulation Language, or DML, manipulates data.
  3. DCL (Data Control Language): Controls access. Example: GRANT, REVOKE.
  4. TCL (Transaction Control Language): Manages transactions. Example: COMMIT, ROLLBACK.

WHERE vs HAVING Clause

  • WHERE filters rows before grouping.
  • HAVING filters after grouping.

Example:

  • WHERE: “Give me employees whose salary is more than 50k.”
  • HAVING: “Show me departments where the average salary is above 60k.”

Finding the Second Highest Salary

There are two popular ways:

Using Subquery:

SELECT MAX(salary)  
FROM employees  
WHERE salary < (SELECT MAX(salary) FROM employees);

Using DENSE_RANK:

SELECT salary  
FROM (  
  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank_salary  
  FROM employees  
) t  
WHERE rank_salary = 2;

What is a JOIN?

A JOIN connects data from two or more tables. Imagine you have a students table and a courses table — a JOIN can tell you which student enrolled in which course.

  • INNER JOIN: Only common records.
  • LEFT JOIN: All of the left table plus the right table’s matches.
  • RIGHT JOIN: Everything on the right plus everything on the left.
  • FULL JOIN: All of both, whether or not they match.
  • CROSS JOIN: Every combination (like multiplying tables).

Tips to Optimize Slow SQL Queries

  • Use indexes for faster lookups.
  • Avoid SELECT * — choose only needed columns.
  • Apply filters early.
  • To identify bottlenecks, use EXPLAIN or execution plans.
  • Replace subqueries with JOINs when possible.
  • Limit results using LIMIT or pagination.

Primary Key vs Foreign Key

  • Primary Key: Unique ID in a table (like an Aadhaar number).
  • Foreign Key: Connects one table to another (like Aadhaar used in a bank account table).

Together, they maintain relationships between tables.


Understanding Indexes

Indexes are like the index of a book. Instead of flipping every page, you jump directly to the topic.

  • Clustered Index: Sorts actual table rows. Only one allowed per table.
  • Non-Clustered Index: Creates a separate lookup structure. Multiple allowed.

Fetching Top Records from a Table

  • Using LIMIT (MySQL, PostgreSQL):
  • SELECT * FROM employees 
  • ORDER BY salary DESC 
  • LIMIT 5;
  • Using TOP (SQL Server):
  • SELECT TOP 5 * FROM employees 
  • ORDER BY salary DESC;

Interview Mindset for SQL

Interviewers don’t just want correct queries — they want to see how you think logically. Keep your answers simple, explain your reasoning, and avoid over-complicating queries.


Practical SQL Examples for Beginners

Try these small exercises:

  • Find employees who earn more than 50k.
  • Show the highest 3 salaries.
  • List departments with more than 10 employees.

Best Practices to Learn SQL

  • Practice a little daily (even 20 mins helps).
  • Use free tools like MySQL, PostgreSQL, or SQLite.
  • Work on small projects — like creating a personal expense tracker.

Conclusion

SQL is not rocket science. It’s just a way of talking to data. If you master the basics — queries, joins, keys, and indexes — you’ll easily impress interviewers. Remember, practice is the real key. Keep coding, and Part 2 will make you even stronger.


FAQs

Q1: Is SQL hard to learn?
Not at all! If you know Excel, SQL feels like an advanced version with more power.

Q2: Can I get a job just by learning SQL?
Yes, many analyst roles require SQL as a core skill. But combining it with Excel, Python, or Power BI makes you even stronger.

Q3: What’s the best way to practice SQL?
Use sample datasets like employee tables or Kaggle datasets. Write small queries daily.

Q4: Is SQL still relevant in 2025?
Absolutely. Every company still stores data in relational databases, so SQL isn’t going anywhere.

Q5: How much time does it take to master SQL?
Basic queries can be learned in 2–3 weeks. Advanced skills may take a few months of consistent practice.

WhatsApp Group Join Now
Telegram Group Join Now
Farook Mohammad
Farook Mohammad
Articles: 36

Leave a Reply

Your email address will not be published. Required fields are marked *