π§ SQL Flashcards β CLI Edition
Use these to review quickly in a terminal-like format. Keep adding as you level up!
ποΈ Question:
What is a database?
π¦ Answer:
A collection of related information that can be stored, queried, and managed.
ποΈ Question:
What is SQL?
π¦ Answer:
A language used to manage and query relational databases (RDBMS).
ποΈ Question:
What is NoSQL?
π¦ Answer:
A category of non-relational databases used for unstructured or semi-structured data.
ποΈ Question:
What is the MySQL equivalent on Arch Linux?
π¦ Answer:
MariaDB is a drop-in replacement for MySQL on Arch Linux.
ποΈ Question:
What is an RDBMS?
π¦ Answer:
Relational Database Management System β stores data in tables with relationships.
ποΈ Question:
What is an NRDBMS?
π¦ Answer:
Non-relational Database Management System β stores data as documents, key-values, or graphs.
ποΈ Question:
How do you create a new table?
π¦ Answer:
Use the CREATE TABLE statement with column names, types, and constraints.
Example:
CREATE TABLE users (id INT, name VARCHAR(100));
ποΈ Question:
How do you see the structure of a table?
π¦ Answer:
Use: DESCRIBE table_name;
ποΈ Question:
How do you add a column to an existing table?
π¦ Answer:
ALTER TABLE table_name ADD column_name datatype;
ποΈ Question:
How do you modify a column's datatype?
π¦ Answer:
ALTER TABLE table_name MODIFY column_name new_datatype;
ποΈ Question:
How do you delete a column from a table?
π¦ Answer:
ALTER TABLE table_name DROP COLUMN column_name;
ποΈ Question:
How do you delete a table?
π¦ Answer:
DROP TABLE table_name;
ποΈ Question:
How do you view all data from a table?
π¦ Answer:
SELECT * FROM table_name;
ποΈ Question:
How do you insert data into a table?
π¦ Answer:
INSERT INTO table_name (col1, col2) VALUES (val1, val2);
ποΈ Question:
What does NOT NULL do?
π¦ Answer:
Ensures a column cannot have NULL values.
ποΈ Question:
What does UNIQUE do?
π¦ Answer:
Ensures all values in a column are unique.
ποΈ Question:
What does DEFAULT do?
π¦ Answer:
Sets a default value for a column if no value is provided on insert.
ποΈ Question:
What does AUTO_INCREMENT do?
π¦ Answer:
Automatically increases the value of a numeric column (usually a primary key) on each new insert.
ποΈ Question:
What is a Primary Key?
π¦ Answer:
A column (or combination) that uniquely identifies each row in a table. Cannot be NULL or duplicated.
ποΈ Question:
What is a Foreign Key?
π¦ Answer:
A column that refers to the Primary Key in another table to maintain relational integrity.
ποΈ Question:
What is a Surrogate Key?
π¦ Answer:
An artificially generated key (like AUTO_INCREMENT or UUID) used to uniquely identify a record without business meaning.
ποΈ Question:
What is a Composite Key?
π¦ Answer:
A Primary Key made up of multiple columns together to uniquely identify a row.
ποΈ Question:
What is a Candidate Key?
π¦ Answer:
A column (or group) that could serve as a Primary Key β it's unique and non-null.
ποΈ Question:
What is an Alternate Key?
π¦ Answer:
A Candidate Key that was not chosen as the Primary Key but is still unique and can identify rows.
ποΈ Question:
What SQL statement is used to modify existing records in a table?
π¦ Answer:
UPDATE
ποΈ Question:
What clause must you include with UPDATE to avoid changing all rows?
π¦ Answer:
WHERE clause
ποΈ Question:
What happens if you run UPDATE students SET name = 'Tanvir'; without WHERE?
π¦ Answer:
It updates the name field for all rows in the students table.
ποΈ Question:
What's the basic syntax of the UPDATE statement?
π¦ Answer:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
ποΈ Question:
How do you remove records from a table in SQL?
π¦ Answer:
Using the DELETE statement
ποΈ Question:
Write a SQL query to delete a student whose id is 5.
π¦ Answer:
DELETE FROM students
WHERE id = 5;
ποΈ Question:
What happens if you run DELETE FROM students; without a WHERE clause?
π¦ Answer:
It deletes all rows from the students table.
ποΈ Question:
Which SQL keyword starts a transaction to safely test updates or deletes?
π¦ Answer:
START TRANSACTION
ποΈ Question:
What SQL command undoes changes made during a transaction?
π¦ Answer:
ROLLBACK
ποΈ Question:
Whatβs the safest practice before running UPDATE or DELETE?
π¦ Answer:
Run a SELECT with the same WHERE clause to preview affected rows.
ποΈ Question:
What does the wildcard % do in SQL?
π¦ Answer:
Matches zero or more characters in a LIKE pattern.
Example: 'A%' matches anything starting with A.
ποΈ Question:
Whatβs the purpose of UNION in SQL?
π¦ Answer:
Combines result sets from two SELECT queries into one, removing duplicates.
ποΈ Question:
How does an INNER JOIN work?
π¦ Answer:
Returns only the rows where there is a match in both joined tables.
ποΈ Question:
Whatβs the difference between LEFT JOIN and RIGHT JOIN?
π¦ Answer:
LEFT JOIN returns all rows from the left table and matched rows from the right.
RIGHT JOIN does the opposite.
ποΈ Question:
What does ON DELETE SET NULL do?
π¦ Answer:
When a referenced row is deleted, it sets the foreign key column to NULL.
ποΈ Question:
What does ON DELETE CASCADE do?
π¦ Answer:
Automatically deletes rows in child table when the referenced row in parent table is deleted.
ποΈ Question:
Whatβs a nested query (subquery)?
π¦ Answer:
A query within another query, often used to filter or compute dynamic conditions.
ποΈ Question:
What does COUNT(*) do in SQL?
π¦ Answer:
Returns the total number of rows in a table.
ποΈ Question:
How do you sort query results?
π¦ Answer:
Using ORDER BY clause: ORDER BY column ASC/DESC;
ποΈ Question:
How do you limit the number of rows in a result?
π¦ Answer:
Using the LIMIT clause. Example: SELECT * FROM users LIMIT 10;
πΎ Keep these in mind when designing tables or answering DBMS interview questions.