Quick Revision

GK One-Line Question & Answer

15541+ short questions with short answers, covering every category and sub-category on the site — no long articles to scroll through. Good for a fast recap before an exam, or a few minutes of daily practice.

DBMS → SQL Basics 16

What is the SQL PIVOT operation conceptually and how is it typically implemented?
Transforming row values into column headers converting a narrow table into a wide table - implemented via conditional aggregation (CASE + GROUP BY) in standard SQL
click to copy
What is the COALESCE(expr1, expr2, ..., exprN) function?
Returns the first non-NULL expression from left to right - short-circuits (stops evaluating) once a non-NULL value is found
click to copy
What does the SQL FETCH FIRST n ROWS ONLY clause do and which standard introduced it?
It limits the result set to the first n rows (equivalent to LIMIT n in MySQL/PostgreSQL), introduced by SQL:2008 standard
click to copy
What is the SQL MERGE statement (also called UPSERT) used for?
Performing INSERT, UPDATE, or DELETE operations in a single statement based on whether a match exists between source and target tables - useful for ETL and synchronization operations
click to copy
What is three-valued logic (3VL) in SQL and what are the three truth values?
TRUE, FALSE, and UNKNOWN - where UNKNOWN results from comparisons involving NULL values; logical operations follow specific rules: TRUE AND UNKNOWN = UNKNOWN, FALSE AND UNKNOWN = FALSE, TRUE OR UNKNOWN = TRUE
click to copy
What is the difference between TRUNCATE and DELETE without a WHERE clause in SQL?
TRUNCATE removes all rows without logging individual row deletions (faster, minimal logging, resets auto-increment), cannot be rolled back in some DBMS, and cannot have triggers. DELETE logs each row deletion (slower, fully transactional, triggers fire, can be rolled back)
click to copy
In SQL what does DISTINCT do when used inside an aggregate function like COUNT(DISTINCT column)?
It counts only unique non-NULL values of the column eliminating duplicates before counting - e.g. COUNT(DISTINCT dept_id) counts how many distinct departments have employees
click to copy
What is the purpose of SQL CHECK constraint and what are its limitations?
To enforce a condition that must be true for all rows in a table; limitations include: cannot reference other tables, cannot contain subqueries in standard SQL, and in some DBMS it was parsed but not enforced
click to copy
What does SELECT DISTINCT department_id FROM employees return differently from SELECT department_id FROM employees GROUP BY department_id?
DISTINCT returns unique values without aggregation capability; GROUP BY allows adding aggregate functions. However, for just listing unique values with no aggregation, they produce identical results - GROUP BY is more powerful but DISTINCT is cleaner for simple deduplication
click to copy
What is the BETWEEN operator in SQL and is it inclusive or exclusive of boundaries?
BETWEEN a AND b is INCLUSIVE of both endpoints - equivalent to >= a AND <= b (both a and b are included in the range)
click to copy
What is the SQL LIKE operator, and what do the wildcards % and _ represent?
% matches zero or more characters (any sequence); _ matches exactly one character (any single character) - used for pattern matching in strings
click to copy
What is the difference between INNER JOIN and CROSS JOIN in SQL?
INNER JOIN returns only rows with matching values in both tables based on a join condition; CROSS JOIN returns the Cartesian product (every combination of rows from both tables, no join condition)
click to copy
What is a recursive CTE (WITH RECURSIVE) in SQL and what problem does it solve?
A CTE that references itself enabling traversal of hierarchical/graph data (like org charts, bill of materials, file systems) without knowing the depth in advance - queries tree/graph structures iteratively until no more rows are added
click to copy
What is a window function in SQL and how does it differ from aggregate functions?
Window functions perform calculations across a set of rows related to the current row without collapsing them into a single result row - unlike aggregate functions which collapse groups into single rows
click to copy
What is the difference between a subquery and a derived table (inline view) in SQL?
A subquery is any query nested within another query; a derived table (inline view) is specifically a subquery in the FROM clause that acts as a named temporary table for the outer query - derived tables require an alias
click to copy
What is the SQL HAVING clause and why is it necessary (can WHERE replace it)?
HAVING filters groups after GROUP BY aggregation and can reference aggregate function results (e.g. HAVING COUNT(*) > 5); WHERE cannot reference aggregate functions because it runs before aggregation - HAVING is necessary for filtering based on aggregated data
click to copy

DBMS → DDL Commands 24

What is the difference between DROP TABLE and TRUNCATE TABLE?
DROP TABLE is DDL - removes the table structure, all data, indexes, triggers, and constraints permanently; TRUNCATE TABLE is DDL in most DBMS - removes all data but preserves the table structure, indexes, and column definitions
click to copy
What does ON DELETE CASCADE in a FOREIGN KEY constraint specify?
When a referenced row in the parent table is deleted, automatically delete all corresponding rows in the child table that reference it
click to copy
What is the difference between ALTER TABLE MODIFY and ALTER TABLE CHANGE in MySQL?
MODIFY changes a column definition (data type, constraints) but keeps the column name; CHANGE can rename the column AND change its definition (requires specifying the new column definition even if unchanged)
click to copy
What is the purpose of CREATE TABLE AS SELECT (CTAS) and what is NOT copied?
Creates a new physical table populated with data from the SELECT query results - but does NOT copy constraints (primary keys, foreign keys, indexes, check constraints) from the source
click to copy
What is a SQL DOMAIN concept and how does it differ from a simple column data type?
A domain is a named reusable column specification that combines a data type with optional constraints (DEFAULT, CHECK) allowing the domain to be used across multiple tables for consistency - defined with CREATE DOMAIN in standard SQL
click to copy
What is the purpose of the DEFERRABLE INITIALLY DEFERRED constraint option in SQL?
Constraints are checked at the end of a transaction (COMMIT) rather than after each statement allowing intermediate states to violate the constraint during a transaction
click to copy
What is CREATE INDEX and what is the difference between a unique index and a non-unique index?
A unique index enforces that no two rows have the same indexed column values (effectively a UNIQUE constraint); a non-unique index allows duplicate values and is used purely for query performance optimization
click to copy
What is the purpose of the GENERATED ALWAYS AS clause in CREATE TABLE (computed columns)?
It defines a column whose value is automatically computed from an expression and stored (STORED) or computed on-the-fly (VIRTUAL) cannot be manually set or updated
click to copy
What does CREATE SCHEMA do and how does it relate to CREATE DATABASE?
CREATE SCHEMA creates a logical namespace/grouping for database objects within a database; CREATE DATABASE creates an entirely new database instance. In some DBMS (PostgreSQL) SCHEMA is a namespace within a database; in MySQL SCHEMA and DATABASE are synonymous
click to copy
What is the effect of adding a NOT NULL constraint to an existing column that already contains NULL values?
The ALTER TABLE command fails with an error because existing NULLs would violate the constraint - you must first update NULLs to valid values before adding the NOT NULL constraint
click to copy
What does the RESTRICT option do in DROP TABLE RESTRICT?
It prevents dropping the table if any other database objects depend on it (views, foreign keys, triggers) requiring dependencies to be removed first
click to copy
What is a partitioned table in DDL and how is RANGE partitioning defined?
A table divided into smaller physical storage units (partitions) based on partition key values; RANGE partitioning: each partition holds rows where partition key falls within specified ranges
click to copy
What is the SQL CREATE TYPE statement used for in object-relational DBMS?
To define user-defined types (UDTs) including structured types with attributes and methods array types row types and enum types - extending the built-in type system
click to copy
What is DDL replication in the context of database replication architecture?
Replicating structural changes (CREATE TABLE, ALTER TABLE, DROP INDEX, etc.) from primary to replica databases ensuring schema stays synchronized - not all replication solutions support DDL replication
click to copy
What is the purpose of CREATE SEQUENCE in SQL and how does it differ from AUTO_INCREMENT?
SEQUENCE is a database object that generates unique sequential numbers independently of any table - can be used across multiple tables allows fine-grained control (INCREMENT BY, START WITH, MINVALUE, MAXVALUE, CYCLE); AUTO_INCREMENT is a column property tied to a specific table
click to copy
What happens when you execute ALTER TABLE ADD COLUMN col INT DEFAULT 5 NOT NULL on a table with millions of existing rows?
In most DBMS this requires rewriting the entire table to add the column value to every row (table-level lock, long operation). PostgreSQL 11+ handles DEFAULT WITHOUT NULL as instant; MySQL 8.0+ supports instant ADD COLUMN for some cases
click to copy
What is CREATE TEMPORARY TABLE and what is its scope?
Creates a table that is visible only to the current session and is automatically dropped when the session ends or connection closes
click to copy
What is the purpose of GRANT and REVOKE statements in SQL and which classification do they fall under?
They are DCL (Data Control Language) statements - GRANT gives specific privileges (SELECT, INSERT, UPDATE, DELETE, EXECUTE) to users/roles on database objects; REVOKE removes those privileges
click to copy
What is the SQL standard way to add a FOREIGN KEY to an existing table?
ALTER TABLE child ADD CONSTRAINT fk_name FOREIGN KEY (child_col) REFERENCES parent_table(parent_col) ON DELETE CASCADE ON UPDATE CASCADE
click to copy
What is online schema change (OSC) and why is it needed?
A technique/tool that allows schema modifications on large production tables without long table locks or downtime typically by creating a shadow table copying data incrementally and atomically swapping the tables
click to copy
What is the purpose of CREATE ASSERTION statement in standard SQL?
A named constraint that can span multiple tables (unlike table-level CHECK constraints which are limited to one table) - allows complex business rules to be defined at the schema level
click to copy
What does INHERITS clause do in PostgreSQL CREATE TABLE?
Creates a child table that inherits all columns and constraints from a parent table with child-specific additional columns - part of PostgreSQLs table inheritance feature for object-relational capabilities
click to copy
In DDL what is the difference between CONSTRAINT constraint_name PRIMARY KEY defined at column level vs table level?
Column-level: can only define single-column constraints; Table-level: can define single or multi-column (composite) constraints and allows explicit naming at the table level - functionally equivalent for single-column but ONLY table-level syntax works for composite keys
click to copy
What is the effect of DROP COLUMN on a table with indexes and foreign keys referencing that column?
Most DBMS automatically drop all indexes that include the dropped column and reject the operation if any foreign key references the dropped column (requiring you to drop the FK first) - or CASCADE can be used to drop dependent objects too
click to copy