Filter by the role you are applying for, open a question, and practise explaining the answer out loud. Mark it practised when you can.
Why they ask this: Joining tables is daily backend work and a staple of online tests in Nepal.
Answer
NULL.UNION.-- Every company, with its job count (including companies with no jobs)
SELECT c.name, COUNT(j.id) AS jobs
FROM companies c
LEFT JOIN jobs j
Why they ask this: Core relational design knowledge for any backend role.
Answer
NULL, and a table has only one (it can span several columns, a composite key).ON DELETE CASCADE or SET NULL to decide what happens when the parent row is deleted.email. A table can have many. In MySQL and PostgreSQL a unique column can hold several NULLs.CREATE TABLE jobs (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
company_id BIGINT NOT NULL,
slug VARCHAR(255) NOT NULL
Why they ask this: Indexes are the first tool for slow queries; this checks real-world database thinking.
Answer
Why they ask this: A favourite academic and interview topic in Nepal; it also shows you can design clean schemas.
Answer
Why they ask this: A common online-test question that checks you understand how GROUP BY queries run.
Answer
Why they ask this: Payments and orders must never be half-saved; this checks you can protect data consistency.
Answer
Note that COUNT(j.id) counts only matched rows, while COUNT(*) would count 1 for a company with no jobs.
An index is a separate data structure (usually a B-tree) that lets the database find rows without scanning the whole table, much like the index at the back of a book.
Benefits: much faster WHERE, JOIN, ORDER BY and lookups on the indexed columns.
Trade-offs:
INSERT, UPDATE, DELETE) get slower because every index must be updated too.Composite indexes follow the leftmost-prefix rule: an index on (status, created_at) helps WHERE status = ? and WHERE status = ? ORDER BY created_at, but not WHERE created_at > ? alone.
CREATE INDEX jobs_status_created_at_index ON jobs (status, created_at);
EXPLAIN SELECT * FROM jobs WHERE status = 'open' ORDER BY created_at DESC;Use EXPLAIN to check whether a query actually uses an index.
Normalisation organises tables to remove duplicate data and the update problems it causes.
skills = "PHP, SQL"; use a separate row per skill.(student_id, course_id) → student_name, the name depends only on student_id, so move it to a students table.employees stores department_id and department_name, move the name to a departments table.Sometimes you denormalise on purpose for read performance, for example by storing a jobs_count on companies. Do it knowingly and keep the copy in sync.
WHERE filters rows before grouping. It cannot use aggregate functions.HAVING filters groups after GROUP BY. It can use aggregates such as COUNT and AVG.-- Companies in Kathmandu with more than 5 open jobs
SELECT company_id, COUNT(*) AS open_jobs
FROM jobs
WHERE status = 'open' AND city = 'Kathmandu' -- row filter
GROUP BY company_id
HAVING COUNT(*) > 5; -- group filterThe logical order is FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT. Filter in WHERE whenever you can, because it reduces the rows that need grouping.
A transaction groups several statements so that they all succeed or all fail together.
ACID:
DB::transaction(function () use ($order, $payment) {
$payment->update(['status' => 'paid']);
$order->update(['status' => 'confirmed']);
$order->product->decrement('stock', $order->quantity);
}); // any exception rolls back all threeClassic example: a bank transfer debits one account and credits another. Both must happen, or neither.