Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
SQL Server: Developed by Microsoft, it's known for its strong integration with other Microsoft products. It’s commonly used in enterprise environments. PostgreSQL: An open-source, object-relational database known for its…
nd vice versa. This often requires a junction table. What interviewers expect A clear definition tied to SQL in SQL & Databases projects Trade-offs (performance, maintainability, security, cost) When you would and wo…
One-to-One (1:1): Each row in one table is linked to one row in another table. One-to-Many (1:M): A row in one table can be linked to many rows in another table. Many-to-Many (M:N): Rows in one table can be linked to man…
Answer: composite index is an index that involves more than one column in a table. It's used when queries often filter or sort by multiple columns, optimizing performance for those specific queries. What interviewers exp…
INNER JOIN: Returns only the rows that have matching values in both tables. LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table and the matching rows from the right table. If no match is found, NULLs are…
Window functions allow you to perform calculations across a set of table rows that are related to the current row, without collapsing the result set into a single row. Example: ROW_NUMBER() generates a sequential integer…
ggregate functions perform a calculation on a set of values and return a single value. Common aggregate functions include: COUNT(): Returns the number of rows. SUM(): Returns the sum of a column. AVG(): Returns the avera…
Data Storage: Table: Stores actual data in the database. View: A virtual table that shows data from one or more tables but does not store data. It derives its data from the underlying tables each time it is queried. Modi…
ccess to the data without revealing the full database schema. Security: By granting access to a view instead of a table, you can restrict access to sensitive data and only expose the columns or rows that are necessary. C…
Simplifies Complex Queries: Views can encapsulate complex queries, making them reusable and easier to manage. Data Abstraction: Views abstract the underlying table structure and allow for easier access to the data withou…
Yes, views can be updated in SQL, but there are conditions for when this is possible: Updatable Views: A view is updatable if it: Is based on a single table. Does not contain aggregation functions like COUNT(), AVG(), or…
Under what conditions? Yes, views can be updated in SQL, but there are conditions for when this is possible: Updatable Views: A view is updatable if it: Is based on a single table. Does not contain aggregation functions…
Answer: re certain DBMS-specific features and optimizations (e.g., indexed views in SQL Server). What interviewers expect A clear definition tied to SQL in SQL & Databases projects Trade-offs (performance, maintainab…
Here’s how you can create a view in each of these DBMS: SQL Server: CREATE VIEW view_name AS SELECT column1, column2 FROM table_name WHERE condition; PostgreSQL: CREATE VIEW view_name AS SELECT column1, column2 FROM tabl…
materialized view is a database object that stores the result of a query physically, similar to an indexed view but more generalized. Unlike regular views, which generate their result dynamically when queried, materializ…
To delete a view, you use the DROP VIEW statement. This removes the view from the database entirely. Example: DROP VIEW view_name; Be cautious when dropping a view, as it will no longer be available for use in queries. E…
Optimizing views is important to ensure that your queries are efficient. Here are some strategies: Avoid Complex Views: Avoid creating views with complex logic (e.g., multiple JOINs, GROUP BY, or subqueries), as they can…
BEGIN SELECT * FROM employees WHERE id = @emp_id; END; PostgreSQL: CREATE OR REPLACE FUNCTION GetEmployeeDetails(emp_id INT) RETURNS TABLE(id INT, name VARCHAR) AS $$ BEGIN RETURN QUERY SELECT id, name FROM employees WHE…
SQL Server: CREATE PROCEDURE GetEmployeeDetails (@emp_id INT) BEGIN SELECT * FROM employees WHERE id = @emp_id; END; PostgreSQL: CREATE OR REPLACE FUNCTION GetEmployeeDetails(emp_id INT) RETURNS TABLE(id INT, name VARCHA…
Performance: Stored procedures are precompiled, so execution is faster compared to running individual SQL queries each time. Code Reusability: Once defined, stored procedures can be reused in multiple places, reducing re…
BEGIN SELECT salary FROM employees WHERE id = @emp_id; END; Calling the procedure: EXEC GetEmployeeSalary @emp_id = 101; MySQL: DELIMITER // CREATE PROCEDURE GetEmployeeSalary(IN emp_id INT) BEGIN SELECT salary FROM empl…
Stored procedures allow you to pass parameters to them, either input or output parameters. Input Parameters: These allow you to send data to the procedure. Output Parameters: These allow the procedure to send data back t…
Error handling is an essential part of stored procedures. Here’s how you handle errors in different databases: SQL Server: Use TRY...CATCH blocks to handle errors. BEGIN TRY - Some SQL operation INSERT INTO employees (na…
You can call stored procedures from programming languages using appropriate database connectors and drivers. Example (C# with SQL Server): using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); u…
wait conn.OpenAsync(); using (SqlCommand cmd = new SqlCommand("GetEmployeeDetails", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@emp_id", SqlDbType.Int)).Value = 101; usin…
SQL & Databases SQL Server Tutorial · SQL
Microsoft products. It’s commonly used in enterprise environments.
compliance, extensibility, and advanced features like support for complex queries,
JSONB, and custom data types.
It's often used in web applications (e.g., with PHP) and is less feature-rich than
PostgreSQL but highly reliable.
SQL & Databases SQL Server Tutorial · SQL
nd vice versa. This often requires a junction table.
In a production SQL & Databases application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
SQL & Databases SQL Server Tutorial · SQL
and vice versa. This often requires a junction table.
SQL & Databases SQL Server Tutorial · SQL
Answer: composite index is an index that involves more than one column in a table. It's used when queries often filter or sort by multiple columns, optimizing performance for those specific queries.
In a production SQL & Databases application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
SQL & Databases SQL Server Tutorial · SQL
matching rows from the right table. If no match is found, NULLs are returned for
columns from the right table.
SQL & Databases SQL Server Tutorial · SQL
Window functions allow you to perform calculations across a set of table rows that are
related to the current row, without collapsing the result set into a single row.
Example: ROW_NUMBER() generates a sequential integer to each row within the result set.
Example:
SELECT name, salary,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num
FROM employees;
This query adds a sequential row number to each employee, ordered by salary.
SQL & Databases SQL Server Tutorial · SQL
ggregate functions perform a calculation on a set of values and return a single value.
Common aggregate functions include:
Example:
SELECT AVG(salary)
FROM employees;
SQL & Databases SQL Server Tutorial · SQL
store data. It derives its data from the underlying tables each time it is
queried.
Some views (those based on a single table) are updatable, while others
(especially those with JOINs, aggregations, or complex logic) are not.
and so on.
time the view is accessed, especially if it's a complex view or involves large
datasets.
SQL & Databases SQL Server Tutorial · SQL
ccess to the data without revealing the full database schema.
sensitive data and only expose the columns or rows that are necessary.
or applications, ensuring consistent results.
you can create a view to encapsulate it and reuse the view in different queries.
SQL & Databases SQL Server Tutorial · SQL
them reusable and easier to manage.
access to the data without revealing the full database schema.
sensitive data and only expose the columns or rows that are necessary.
or applications, ensuring consistent results.
you can create a view to encapsulate it and reuse the view in different queries.
SQL & Databases SQL Server Tutorial · SQL
Yes, views can be updated in SQL, but there are conditions for when this is possible:
that modify the set of rows.
clauses are typically non-updatable.
You can still modify data through these views by using triggers, such as INSTEAD
OF triggers, which allow you to define custom behavior for updating or deleting rows.
Example of an updatable view:
CREATE VIEW simple_view AS
SELECT id, name, salary FROM employees;
UPDATE simple_view SET salary = 60000 WHERE id = 101;SQL & Databases SQL Server Tutorial · SQL
Under what conditions?
Yes, views can be updated in SQL, but there are conditions for when this is possible:
that modify the set of rows.
clauses are typically non-updatable.
You can still modify data through these views by using triggers, such as INSTEAD
OF triggers, which allow you to define custom behavior for updating or deleting rows.
Example of an updatable view:
CREATE VIEW simple_view AS
SELECT id, name, salary FROM employees;
UPDATE simple_view SET salary = 60000 WHERE id = 101;
SQL & Databases SQL Server Tutorial · SQL
Answer: re certain DBMS-specific features and optimizations (e.g., indexed views in SQL Server).
In a production SQL & Databases application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
SQL & Databases SQL Server Tutorial · SQL
Here’s how you can create a view in each of these DBMS:
SQL Server:
CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition;
PostgreSQL:
CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition;
MySQL:
CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition;
The syntax for creating a view is generally the same across these databases, although there
are certain DBMS-specific features and optimizations (e.g., indexed views in SQL Server).
SQL & Databases SQL Server Tutorial · SQL
materialized view is a database object that stores the result of a query physically, similar
to an indexed view but more generalized. Unlike regular views, which generate their result
dynamically when queried, materialized views store the result set and can be periodically
refreshed.
since the data is precomputed and stored.
needs to be refreshed periodically, either manually or automatically, depending on the
DBMS.
Example:
CREATE MATERIALIZED VIEW sales_summary AS
SELECT product_id, SUM(sales) AS total_sales
FROM sales
GROUP BY product_id;
REFRESH MATERIALIZED VIEW sales_summary;
not have a built-in materialized view feature. You can simulate one using tables and
scheduled jobs.
SQL & Databases SQL Server Tutorial · SQL
To delete a view, you use the DROP VIEW statement. This removes the view from the
database entirely.
Example:
DROP VIEW view_name;
Be cautious when dropping a view, as it will no longer be available for use in queries. Ensure
no other objects are dependent on the view before dropping it.
SQL & Databases SQL Server Tutorial · SQL
Optimizing views is important to ensure that your queries are efficient. Here are some
strategies:
JOINs, GROUP BY, or subqueries), as they can result in slower performance. Keep
views simple and focused.
columns used in JOIN or WHERE conditions.
performing heavy aggregations. These views store the results physically and can be
refreshed periodically.
columns. Only select the data that is necessary.
those tables are optimized (e.g., with appropriate indexing and normalization).
as the inner view’s query needs to be executed each time the outer view is queried.
Stored Procedures & Functions
SQL & Databases SQL Server Tutorial · SQL
BEGIN
SELECT * FROM employees WHERE id = @emp_id;
END;
CREATE OR REPLACE FUNCTION GetEmployeeDetails(emp_id INT)
RETURNS TABLE(id INT, name VARCHAR) AS $$
BEGIN
RETURN QUERY SELECT id, name FROM employees WHERE id = emp_id;
END;
$$ LANGUAGE plpgsql;
DELIMITER //
CREATE PROCEDURE GetEmployeeDetails(IN emp_id INT)
BEGIN
SELECT * FROM employees WHERE id = emp_id;
END //
DELIMITER ;
SQL & Databases SQL Server Tutorial · SQL
SQL Server:
CREATE PROCEDURE GetEmployeeDetails (@emp_id INT)
BEGIN
SELECT * FROM employees WHERE id = @emp_id;
END;
PostgreSQL:
CREATE OR REPLACE FUNCTION GetEmployeeDetails(emp_id INT)
RETURNS TABLE(id INT, name VARCHAR) AS $$
BEGIN
RETURN QUERY SELECT id, name FROM employees WHERE id = emp_id;
END;
$$ LANGUAGE plpgsql;
MySQL:
DELIMITER //
CREATE PROCEDURE GetEmployeeDetails(IN emp_id INT)
BEGIN
SELECT * FROM employees WHERE id = emp_id;
END //
DELIMITER ;
The syntax varies slightly between the databases, but the core idea remains the same.
SQL & Databases SQL Server Tutorial · SQL
to running individual SQL queries each time.
places, reducing redundancy.
giving them direct access to the underlying tables.
maintenance easier, especially when making changes to the logic.
like TRY...CATCH in SQL Server or EXCEPTION in PostgreSQL.
SQL & Databases SQL Server Tutorial · SQL
BEGIN
SELECT salary FROM employees WHERE id = @emp_id;
END;
EXEC GetEmployeeSalary @emp_id = 101;
DELIMITER //
CREATE PROCEDURE GetEmployeeSalary(IN emp_id INT)
BEGIN
SELECT salary FROM employees WHERE id = emp_id;
END //
DELIMITER ;
CALL GetEmployeeSalary(101);
SQL & Databases SQL Server Tutorial · SQL
Stored procedures allow you to pass parameters to them, either input or output
parameters.
Example:
SQL Server:
CREATE PROCEDURE GetEmployeeSalary (@emp_id INT)
BEGIN
SELECT salary FROM employees WHERE id = @emp_id;
END;
Calling the procedure:
EXEC GetEmployeeSalary @emp_id = 101;
MySQL:
DELIMITER //
CREATE PROCEDURE GetEmployeeSalary(IN emp_id INT)
BEGIN
SELECT salary FROM employees WHERE id = emp_id;
END //
DELIMITER ;
Calling the procedure:
CALL GetEmployeeSalary(101);
SQL & Databases SQL Server Tutorial · SQL
Error handling is an essential part of stored procedures. Here’s how you handle errors in
different databases:
SQL Server: Use TRY...CATCH blocks to handle errors.
BEGIN TRY
INSERT INTO employees (name) VALUES ('John Doe');
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
BEGIN
INSERT INTO employees (name) VALUES ('John Doe');
EXCEPTION
WHEN others THEN
RAISE NOTICE 'Error occurred: %', SQLERRM;
END;
DECLARE...HANDLER.
DELIMITER //
CREATE PROCEDURE ExampleProcedure()
BEGIN
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SELECT 'An error occurred';
INSERT INTO employees (name) VALUES ('John Doe');
END //
DELIMITER ;
SQL & Databases SQL Server Tutorial · SQL
You can call stored procedures from programming languages using appropriate database
connectors and drivers.
Example (C# with SQL Server):
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("GetEmployeeDetails",
conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@emp_id",
SqlDbType.Int)).Value = 101;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader["name"]);
}
}
}
}SQL & Databases SQL Server Tutorial · SQL
wait conn.OpenAsync();
using (SqlCommand cmd = new SqlCommand("GetEmployeeDetails",
conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@emp_id",
SqlDbType.Int)).Value = 101;
using (SqlDataReader reader = await
cmd.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
Console.WriteLine(reader["name"]);
}
}
}
}