1.what is index
A database index is a performance optimization tool that allows the database to find rows faster when you query by specific columns.
CREATE INDEX idx_users_email ON users(email);
idx_users_email is the name of the index.
It improves performance for queries filtering by email.
π Types of Indexes
| Type | Description |
| Single-column index | Index on one column (e.g., email) |
| Multi-column (composite) | Index on multiple columns (e.g., first_name, last_name) |
| Unique index | Prevents duplicate values |
| Full-text index | Used for keyword searches in text |
| Primary key | Automatically indexed |
| Foreign key | Often indexed for JOIN performance |
Trade-offs
| Pros | Cons |
| Speeds up SELECT queries | Slows down INSERT/UPDATE/DELETE |
| Helps enforce uniqueness | Takes up extra storage |
| Improves JOIN performance | Needs to be managed carefully |
2.Β what are keys in database
In a database, keys are columns or combinations of columns that are used to identify, link, or enforce rules on data. They’re essential for ensuring data integrity, optimizing performance, and enabling relationships between tables.
- Primary Key
- A unique identifier for each row in a table.
- Cannot be NULL or duplicated.
- Every table should have one primary key.
- Foreign Key
- A column in one table that references the primary key in another table.
- Used to create relationships between tables.
- Unique Key
- Enforces that all values in the column are distinct.
- Like a primary key, but a table can have multiple unique keys.
- Composite Key
- A key made up of multiple columns.
- Uniquely identifies a row using the combination of values.
- Candidate Key
- A column (or set of columns) that can uniquely identify a row.
- The best candidate is chosen as the primary key.
- Others can be considered as alternate keys.
- Super Key
- Any set of columns that can uniquely identify a row.
- All candidate keys are super keys, but not all super keys are candidate keys.
- Index Key (Not a constraint, but performance-related)
- Columns used in an index to speed up searching.
- Often overlaps with primary/foreign/unique keys.
| Key Type | Purpose | Can be Null? | Must Be Unique? | Max per table |
| Primary Key | Uniquely identifies each row | β | β | 1 |
| Foreign Key | Links to another table | β (default) | β | Many |
| Unique Key | Ensures uniqueness | β | β | Many |
| Composite Key | Combines columns as key | β | β | 1 (as PK) |
| Candidate Key | Eligible to be a primary key | β | β | Many |
3. What is diff between MongoDB and MySQL
| Feature | MongoDB | MySQL |
| Type | NoSQL (Document-oriented) | SQL (Relational Database) |
| Data Format | JSON-like documents (BSON) | Structured tables with rows & columns |
| Schema | Schema-less (dynamic fields) | Fixed schema (defined tables) |
| Query Language | MongoDB Query Language (MQL) | SQL (Structured Query Language) |
| Joins | Limited $lookup, not native | Strong native JOIN support |
| Transactions | Supported (since v4.0) | Fully supported |
| Best For | Unstructured or semi-structured data | Structured and relational data |
| Scaling | Horizontal (sharding) | Mostly vertical (scaling up server) |
| Storage | Documents stored in collections | Rows stored in tables |
| Use Case | Fast dev, flexible data (e.g. logs, JSON APIs) | Financial systems, relational apps |
| Performance | Faster with large, unstructured data | Faster with complex queries & joins |
| ACID Compliance | Partial (now full in latest versions) | Full ACID compliance |
β Choose MongoDB When:
- You want fast prototyping or flexible schema
- You deal with JSON or nested data
- You’re building real-time analytics, IoT, or document storage
β Choose MySQL When:
- You need complex queries, joins, and constraints
- You require strict schema and relationships
4.What is Left Join?
- Returns all rows from the left table (table A),
- And the matching rows from the right table (table B).
- If no match is found in the right table, the result will contain NULL for right tableβs columns.
Syntax:
SELECT A.id, A.name, B.order_id
FROM Customers A
LEFT JOIN Orders B
ON A.id = B.customer_id;
5. What is Right Join?
- Returns all rows from the right table (table B),
- And the matching rows from the left table (table A).
- If no match is found in the left table, the result will contain NULL for left tableβs columns.
Syntax:
SELECT A.id, A.name, B.order_id
FROM Customers A
RIGHT JOIN Orders B
ON A.id = B.customer_id;
6. I have Employee table and below column emp_id, dept_id, salary and i want to find 4 employee maximum salry from multiple departments.give sql query for that.
SELECT emp_id, dept_id, salary
FROM (
SELECT
emp_id,
dept_id,
salary,
ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rn
FROM Employee
) AS ranked
WHERE rn <= 4;
7.Diff Between Having vs Where and Primary Key vs Unique key?
WHERE vs HAVING
π WHERE: filters before grouping
π HAVING: filters after GROUP BY
1. Primary Key vs Unique Key
| Primary Key | Unique Key |
| Only one per table | Multiple allowed |
| Cannot be NULL | Can be NULL |
8. Primary key vs Unique key?
- Primary key: unique + NOT NULL + identifies row
- Unique key: unique but can allow NULL (depends DB)
- Foreign key?
β
Answer:
Maintains relationship between tables.
Ensures referenced row exists.
9. Does sql.Open() create a database connection immediately?
No. sql.Open() initializes a *sql.DB, which represents a pool of connections. It validates the configuration but doesn’t necessarily establish a connection immediately. Calling db.Ping() or executing the first query causes an actual connection attempt.
10. Is sql.DB thread-safe?
Yes. A single *sql.DB is safe for concurrent use by multiple goroutines and should generally be shared across your application.
11.What happens if all connections are busy?
If the number of in-use connections reaches MaxOpenConns, additional requests wait until a connection is returned to the pool (or until the operation times out if a context deadline is reached).
12. Should you call sql.Open() for every request?
No. Create one *sql.DB when your application starts and reuse it. Creating a new *sql.DB for every request defeats the purpose of connection pooling and can hurt performance.
For Golang backend interviews, this is one of the most commonly asked database topics. Understanding how database/sql manages connection pooling and how to tune SetMaxOpenConns, SetMaxIdleConns, SetConnMaxLifetime, and SetConnMaxIdleTime is often expected.