Comprehensive Guide: SQL & Python Interview Preparation
Table of Contents
1. SQL Interview Questions
2. Python Interview Questions
1. SQL Interview Questions
1. Basic SELECT
Question: Write a SQL query to get all records from the 'employees' table.
SELECT * FROM employees;
2. WHERE Clause
Question: Get all customers from 'customers' table where country is 'USA'.
SELECT * FROM customers WHERE country = 'USA';
3. ORDER BY
Question: Retrieve all products ordered by price descending.
SELECT * FROM products ORDER BY price DESC;
4. GROUP BY and COUNT
Question: Count the number of orders per customer.
SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id;
5. HAVING
Question: Get products with more than 10 sales.
SELECT product_id, COUNT(*) AS total_sales FROM sales GROUP BY product_id HAVING
COUNT(*) > 10;
6. INNER JOIN
Question: List customers with their orders.
SELECT c.name, o.order_id FROM customers c INNER JOIN orders o ON c.customer_id =
o.customer_id;
Comprehensive Guide: SQL & Python Interview Preparation
7. LEFT JOIN
Question: List all customers and their orders, if any.
SELECT c.name, o.order_id FROM customers c LEFT JOIN orders o ON c.customer_id =
o.customer_id;
8. Subquery
Question: Find products that have never been ordered.
SELECT * FROM products WHERE product_id NOT IN (SELECT product_id FROM orders);
9. UNION
Question: Combine customer and supplier emails.
SELECT email FROM customers UNION SELECT email FROM suppliers;
10. CASE WHEN
Question: Mark orders as 'High' if amount > 1000, else 'Low'.
SELECT order_id, amount, CASE WHEN amount > 1000 THEN 'High' ELSE 'Low' END AS
order_priority FROM orders;
2. Python Interview Questions
1. List Comprehension
Question: Create a list of squares from 1 to 10.
squares = [x**2 for x in range(1, 11)]
2. Dictionary Comprehension
Question: Map numbers to their squares from 1 to 5.
squares_dict = {x: x**2 for x in range(1, 6)}
3. Lambda Function
Question: Write a lambda to add 10 to a number.
add_ten = lambda x: x + 10
Comprehensive Guide: SQL & Python Interview Preparation
4. Filter Function
Question: Filter even numbers from a list.
evens = list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5]))
5. Map Function
Question: Double each number in a list.
doubled = list(map(lambda x: x * 2, [1, 2, 3]))
6. Reduce Function
Question: Compute factorial using reduce.
from functools import reduce
factorial = reduce(lambda x, y: x * y, range(1, 6))
7. Try-Except
Question: Handle division by zero.
try:
x = 1 / 0
except ZeroDivisionError:
print('Cannot divide by zero')
8. Class with __str__
Question: Create a class with a string representation.
class Person:
def __init__(self, name):
self.name = name
def __str__(self):
return f'Person: {self.name}'
9. Read File
Question: Read a file and print its contents.
with open('file.txt', 'r') as f:
print(f.read())
Comprehensive Guide: SQL & Python Interview Preparation
10. Decorator
Question: Write a simple decorator to log function calls.
def log(func):
def wrapper(*args, **kwargs):
print(f'Calling {func.__name__}')
return func(*args, **kwargs)
return wrapper