Basic MySQL Questions
What is MySQL?
- MySQL is an open-source relational database management system (RDBMS) that uses Structured Query Language (SQL) for managing data.
What is a database?
- A database is a structured collection of data that can be accessed, managed, and updated efficiently.
What is SQL?
- SQL (Structured Query Language) is a language used for managing and manipulating relational databases.
What are the different types of SQL commands?
- DDL (Data Definition Language)
- DML (Data Manipulation Language)
- DCL (Data Control Language)
- TCL (Transaction Control Language)
What are some common MySQL data types?
INT,VARCHAR,TEXT,DATE,BOOLEAN,FLOAT,DECIMAL,BLOB
What is the difference between CHAR and VARCHAR?
CHARis a fixed-length string, whileVARCHARis a variable-length string.
How do you create a database in MySQL?
CREATE DATABASE my_database;How do you delete a database?
DROP DATABASE my_database;How do you create a table?
CREATE TABLE users ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), email VARCHAR(100) UNIQUE );How do you delete a table?
DROP TABLE users;How do you insert data into a table?
INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');How do you retrieve all records from a table?
SELECT * FROM users;How do you update a record in a table?
UPDATE users SET name = 'Jane Doe' WHERE id = 1;How do you delete a record from a table?
DELETE FROM users WHERE id = 1;What is the difference between DELETE and TRUNCATE?
DELETEremoves specific rows with a condition.TRUNCATEremoves all rows from a table but keeps the structure intact.
How do you use a WHERE clause?
SELECT * FROM users WHERE name = 'John Doe';How do you sort query results?
SELECT * FROM users ORDER BY name ASC;How do you count the number of rows in a table?
SELECT COUNT(*) FROM users;What is a primary key?
- A primary key is a unique identifier for each row in a table.
What is a foreign key?
- A foreign key is a column that references the primary key of another table.
Intermediate MySQL Questions
What is a JOIN in MySQL?
- A
JOINis used to retrieve data from multiple tables based on a related column.
- A
What are the types of JOINs?
INNER JOIN,LEFT JOIN,RIGHT JOIN,FULL JOIN,SELF JOIN
How do you use an INNER JOIN?
SELECT users.name, orders.order_date FROM users INNER JOIN orders ON users.id = orders.user_id;How do you use a LEFT JOIN?
SELECT users.name, orders.order_date FROM users LEFT JOIN orders ON users.id = orders.user_id;What is the difference between UNION and UNION ALL?
UNIONremoves duplicate rows, whileUNION ALLincludes duplicates.
What is an index in MySQL?
- An index is used to speed up searches in a database.
How do you create an index?
CREATE INDEX idx_name ON users(name);What is normalization?
- Normalization is the process of organizing data to reduce redundancy.
What is a stored procedure?
- A stored procedure is a set of SQL statements saved for reuse.
How do you create a stored procedure?
DELIMITER // CREATE PROCEDURE GetUsers() BEGIN SELECT * FROM users; END // DELIMITER ;What is a trigger in MySQL?
- A trigger is a procedure that executes automatically in response to an event.
What is a view?
- A view is a virtual table based on a SQL query.
How do you create a view?
CREATE VIEW user_view AS SELECT name, email FROM users;How do you delete a view?
DROP VIEW user_view;What is a subquery?
- A subquery is a query inside another query.
How do you use a subquery?
SELECT name FROM users WHERE id IN (SELECT user_id FROM orders);What is MySQL replication?
- Replication allows data from one MySQL server to be copied to another.
What is the difference between MyISAM and InnoDB?
InnoDBsupports transactions, foreign keys, and row-level locking, whileMyISAMis faster but lacks these features.
What is ACID in databases?
- ACID stands for Atomicity, Consistency, Isolation, Durability.
What is the purpose of AUTO_INCREMENT?
- It automatically generates unique numbers for a column.
Advanced MySQL Questions
What is a transaction in MySQL?
- A transaction is a sequence of SQL operations executed as a single unit.
How do you start and commit a transaction?
START TRANSACTION; UPDATE users SET name = 'New Name' WHERE id = 1; COMMIT;How do you rollback a transaction?
ROLLBACK;What is a cursor in MySQL?
- A cursor is used to retrieve row-by-row processing in stored procedures.
What is a deadlock in MySQL?
- A deadlock occurs when two transactions block each other.
How do you optimize a MySQL query?
- Use indexes, limit result sets, and avoid SELECT *.
How do you check database size?
SELECT table_schema "Database", SUM(data_length + index_length) / 1024 / 1024 "Size (MB)" FROM information_schema.tables GROUP BY table_schema;What is a composite key?
- A composite key is a primary key that consists of multiple columns.
How do you backup a MySQL database?
mysqldump -u root -p my_database > backup.sqlHow do you restore a MySQL database?
mysql -u root -p my_database < backup.sql
Advanced MySQL Questions (Continued)
What is the difference between HAVING and WHERE?
WHEREfilters records before aggregation, whileHAVINGfilters after aggregation.
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 10;What is the difference between DISTINCT and GROUP BY?
DISTINCTremoves duplicate values, whileGROUP BYgroups records and can use aggregate functions.
What are aggregate functions in MySQL?
COUNT(),SUM(),AVG(),MIN(),MAX()
How do you find the second highest salary?
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);How do you get the top N records from a table?
SELECT * FROM employees ORDER BY salary DESC LIMIT 5;What is the default port for MySQL?
- Port 3306.
How do you change a column’s data type?
ALTER TABLE users MODIFY COLUMN name TEXT;How do you add a new column to an existing table?
ALTER TABLE users ADD COLUMN age INT;How do you rename a column?
ALTER TABLE users CHANGE COLUMN old_name new_name VARCHAR(100);How do you remove a column from a table?
ALTER TABLE users DROP COLUMN age;What is the difference between NOW() and CURRENT_TIMESTAMP()?
- Both return the current date and time, but
NOW()allows fractional seconds.
- Both return the current date and time, but
What is a CASE statement?
SELECT name, CASE WHEN age < 18 THEN 'Minor' ELSE 'Adult' END AS age_group FROM users;How do you concatenate strings in MySQL?
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;How do you extract a substring in MySQL?
SELECT SUBSTRING('Hello, World!', 1, 5); -- Output: HelloHow do you convert a string to uppercase in MySQL?
SELECT UPPER('hello'); -- Output: HELLOHow do you convert a string to lowercase in MySQL?
SELECT LOWER('HELLO'); -- Output: helloHow do you find the length of a string?
SELECT LENGTH('MySQL'); -- Output: 5How do you replace a substring in MySQL?
SELECT REPLACE('Hello, World!', 'World', 'MySQL'); -- Output: Hello, MySQL!What is the COALESCE function?
- It returns the first non-null value from a list.
SELECT COALESCE(NULL, NULL, 'Hello', 'World'); -- Output: HelloHow do you check for NULL values?
SELECT * FROM users WHERE email IS NULL;How do you set a default value for a column?
ALTER TABLE users ALTER COLUMN status SET DEFAULT 'Active';How do you drop an index?
DROP INDEX idx_name ON users;What is the IFNULL function?
SELECT IFNULL(NULL, 'Default Value'); -- Output: Default ValueWhat is the difference between a clustered and non-clustered index?
- A clustered index stores data physically in the order of the index.
- A non-clustered index maintains a separate structure for searching.
How do you disable foreign key checks?
SET FOREIGN_KEY_CHECKS = 0;How do you enable foreign key checks?
SET FOREIGN_KEY_CHECKS = 1;How do you find duplicate records?
SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1;How do you remove duplicate records?
DELETE u1 FROM users u1 INNER JOIN users u2 WHERE u1.id > u2.id AND u1.email = u2.email;What is a full-text search in MySQL?
- A search technique that allows for complex text-based queries.
How do you create a full-text index?
CREATE FULLTEXT INDEX idx_text ON articles(content);What is a materialized view?
- A materialized view is a stored query result.
What is MySQL Workbench?
- A GUI tool for managing MySQL databases.
How do you convert a timestamp to a date?
SELECT DATE(FROM_UNIXTIME(1672531199));How do you get the current date?
SELECT CURDATE();How do you get the current time?
SELECT CURTIME();How do you calculate the difference between two dates?
SELECT DATEDIFF('2025-01-01', '2024-01-01'); -- Output: 365How do you add days to a date?
SELECT DATE_ADD('2024-01-01', INTERVAL 30 DAY);What is MySQL slow query log?
- A log that tracks queries taking longer than a defined threshold.
How do you enable slow query log?
SET GLOBAL slow_query_log = 'ON';What is the MySQL Query Cache?
- A cache mechanism to store query results for faster retrieval.
What is the MySQL binary log?
- A log file that records all changes to the database.
How do you check running queries?
SHOW PROCESSLIST;How do you kill a running query?
KILL query_id;How do you change the root password in MySQL?
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';How do you flush privileges?
FLUSH PRIVILEGES;How do you check user privileges?
SHOW GRANTS FOR 'user'@'localhost';How do you list all databases?
SHOW DATABASES;How do you list all tables in a database?
SHOW TABLES;How do you optimize a table?
OPTIMIZE TABLE users;How do you repair a table?
sql REPAIR TABLE users;