1. What is MySQL?
Answer: MySQL is an open-source Relational Database Management System (RDBMS) that stores data in
structured form.
2. Which command is used to create a database in MySQL?
Answer: CREATE DATABASE database_name;
3. What does SQL stand for?
Answer: Structured Query Language.
4. Which command is used to create a table in MySQL?
Answer: CREATE TABLE table_name (column_name datatype, ...);
5. How do you retrieve data from a table in MySQL?
Answer: By using the SELECT command.
6. What is a PRIMARY KEY in MySQL?
Answer: A PRIMARY KEY uniquely identifies each record in a table.
7. How do you delete a column from a table in MySQL?
Answer: ALTER TABLE table_name DROP COLUMN column_name;
8. Which command is used to update data in MySQL?
Answer: UPDATE table_name SET column_name=value WHERE condition;
9. What is the default port number for MySQL?
Answer: 3306.
10. Which command is used to delete a table in MySQL?
Answer: DROP TABLE table_name;
11. What is a FOREIGN KEY in MySQL?
Answer: A FOREIGN KEY is a field that links one table to another by referencing the PRIMARY KEY of the
other table.
12. How do you add a new column to an existing table?
Answer: ALTER TABLE table_name ADD column_name datatype;
13. How do you delete a database in MySQL?
Answer: DROP DATABASE database_name;
14. What is the purpose of the WHERE clause in MySQL?
Answer: It is used to filter records based on a specific condition.
15. How can you sort data in ascending order?
Answer: By using ORDER BY column_name ASC;
16. How do you insert data into a table?
Answer: INSERT INTO table_name (column1, column2) VALUES (value1, value2);
17. How can you retrieve unique records from a table?
Answer: By using the DISTINCT keyword.
18. What is the difference between CHAR and VARCHAR in MySQL?
Answer: CHAR has a fixed length, whereas VARCHAR has a variable length.
19. Which command is used to rename a table?
Answer: RENAME TABLE old_name TO new_name;
20. What is the use of the LIKE operator in MySQL?
Answer: It is used to perform pattern matching.
21. How do you display the structure of a table?
Answer: DESCRIBE table_name; or SHOW COLUMNS FROM table_name;
22. What is a NULL value in MySQL?
Answer: A NULL value represents missing or undefined data.
23. How do you count the number of records in a table?
Answer: SELECT COUNT(*) FROM table_name;
24. What does the JOIN clause do?
Answer: It combines rows from two or more tables based on a related column.
25. How do you create an index in MySQL?
Answer: CREATE INDEX index_name ON table_name (column_name);
26. How can you remove duplicate rows in a query result?
Answer: Use SELECT DISTINCT.
27. How do you limit the number of records retrieved?
Answer: SELECT * FROM table_name LIMIT n;
28. What is a COMMIT statement in MySQL?
Answer: It saves all changes made during a transaction.
29. What is the difference between DELETE and TRUNCATE?
Answer: DELETE removes specific rows, while TRUNCATE removes all rows but keeps the table structure.
30. How do you roll back a transaction?
Answer: Use the ROLLBACK command.
31. What is the purpose of the GROUP BY clause?
Answer: It groups rows that have the same values in specified columns.
32. How do you find the maximum value in a column?
Answer: SELECT MAX(column_name) FROM table_name;
33. Which function calculates the total sum of a column?
Answer: SUM() function.
34. How do you find the average value in a column?
Answer: SELECT AVG(column_name) FROM table_name;
35. What does the HAVING clause do?
Answer: It filters groups after the GROUP BY clause.
36. What is the purpose of the AUTO_INCREMENT attribute?
Answer: It generates a unique number automatically for each new record.
37. How do you delete all rows in a table without deleting the table itself?
Answer: TRUNCATE TABLE table_name;
38. What is normalization?
Answer: Normalization is the process of organizing data to reduce redundancy and improve data integrity.
39. What is the use of the AS keyword?
Answer: It renames a table or column temporarily.
40. What is a transaction in MySQL?
Answer: A transaction is a sequence of SQL commands executed as a single unit.
41. How do you set a default value for a column?
Answer: Use the DEFAULT keyword during table creation.
42. Which operator is used to combine results of two SELECT statements?
Answer: UNION operator.
43. How do you find the current date in MySQL?
Answer: SELECT CURDATE();
44. How do you find the current time?
Answer: SELECT CURTIME();
45. How do you retrieve the database version?
Answer: SELECT VERSION();
46. What is a view in MySQL?
Answer: A view is a virtual table based on the result of a query.
47. How do you create a view?
Answer: CREATE VIEW view_name AS SELECT column1, column2 FROM table_name;
48. How can you drop a view?
Answer: DROP VIEW view_name;
49. How do you check all available databases?
Answer: SHOW DATABASES;
50. How do you check all tables in a database?
Answer: SHOW TABLES;
1. What is MySQL and what are its features?
Answer:
MySQL is an open-source Relational Database Management System (RDBMS) based on Structured Query
Language (SQL). It is widely used to manage and store data for applications.
Features of MySQL:
1. Open-source: MySQL is free to use and customize.
2. Cross-platform: It works on various operating systems like Windows, Linux, and macOS.
3. High Performance: MySQL offers fast data retrieval and transactions.
4. Scalability: Supports large databases and high volumes of data.
5. Security: Provides user authentication, access control, and encrypted connections.
6. Replication: MySQL supports database replication for backup and load balancing.
2. Explain the concept of a Relational Database Management System (RDBMS).
Answer:
An RDBMS is a database management system that stores data in tables (rows and columns), where
relationships exist between different tables.
Tables: A collection of rows (records) and columns (fields).
Relationships: Tables are connected using Primary Keys and Foreign Keys.
Integrity Constraints: Ensures accuracy and consistency of data.
Examples of RDBMS include MySQL, Oracle, SQL Server, and PostgreSQL.
3. What are the differences between MySQL and SQL?
Answer:
SQL MySQL
SQL is a language used to interact with databases. MySQL is an RDBMS that uses SQL as its query language.
SQL is not software but a query standard. MySQL is database software.
Platform-independent. Specific implementation on platforms.
SQL is universal. MySQL is specific to its database system.
4. What are the basic SQL commands in MySQL? Explain with examples.
Answer:
SQL commands are categorized into:
1. DDL (Data Definition Language):
o CREATE TABLE table_name (column datatype);
o DROP TABLE table_name;
2. DML (Data Manipulation Language):
o INSERT INTO table_name VALUES (value1, value2);
o UPDATE table_name SET column=value WHERE condition;
3. DCL (Data Control Language):
o GRANT SELECT ON table_name TO user;
4. TCL (Transaction Control Language):
o COMMIT; and ROLLBACK;
5. DQL (Data Query Language):
o SELECT * FROM table_name;
5. Explain the concept of Primary Key and Foreign Key in MySQL.
Answer:
Primary Key: A column (or set of columns) that uniquely identifies each record in a table. It cannot
have NULL values.
Example:
CREATE TABLE Employee (ID INT PRIMARY KEY, Name VARCHAR(50));
Foreign Key: A column that references the Primary Key of another table to establish a relationship
between two tables.
Example:CREATE TABLE Orders (OrderID INT, EmpID INT, FOREIGN KEY (EmpID)
REFERENCES Employee(ID));
6. What is normalization? Explain its types.
Answer:
Normalization is the process of organizing data to minimize redundancy and improve data integrity.
Types of Normalization:
1. 1NF (First Normal Form): Eliminate duplicate columns and ensure each column contains atomic
values.
2. 2NF (Second Normal Form): Meet 1NF and remove partial dependency.
3. 3NF (Third Normal Form): Meet 2NF and remove transitive dependency.
4. BCNF (Boyce-Codd Normal Form): A stricter version of 3NF.
7. How do you create and delete a database in MySQL?
Answer:
Create Database:
CREATE DATABASE database_name;
Delete Database:
DROP DATABASE database_name;
8. What is the purpose of the SELECT statement in MySQL?
Answer:
The SELECT statement is used to retrieve data from a table.
Syntax:
SELECT column1, column2 FROM table_name WHERE condition;
Example: SELECT Name, Salary FROM Employee WHERE Salary > 5000;
9. Explain the use of the JOIN clause in MySQL and its types.
Answer:
The JOIN clause is used to combine rows from two or more tables based on a related column.
Types of JOINs:
1. INNER JOIN: Returns records with matching values.
2. LEFT JOIN: Returns all records from the left table and matching records from the right table.
3. RIGHT JOIN: Returns all records from the right table and matching records from the left table.
4. FULL JOIN: Returns all records when there is a match in either table.
Example:
SELECT A.ID, B.Name
FROM Employee A
INNER JOIN Orders B ON A.ID = B.EmpID;
10. What is the WHERE clause in MySQL? Provide an example.
Answer:
The WHERE clause is used to filter records based on a condition.
Example:
SELECT * FROM Employee WHERE Department = 'Sales';
11. What is the difference between DELETE, TRUNCATE, and DROP?
Answer:
Command Purpose Structure Retained
DELETE Deletes specific rows in a table. Yes
TRUNCATE Deletes all rows in a table. Yes
DROP Deletes the entire table. No
Example:
DELETE FROM table_name WHERE condition;
TRUNCATE TABLE table_name;
DROP TABLE table_name;
12. What is the purpose of indexes in MySQL?
Answer:
Indexes are used to speed up data retrieval operations in a table.
Syntax: CREATE INDEX index_name ON table_name (column_name);
13. Explain transactions in MySQL.
Answer:
A transaction is a sequence of SQL commands that are executed as a single unit.
Commands for Transaction Management:
1. START TRANSACTION;
2. COMMIT; (Save changes)
3. ROLLBACK; (Undo changes)
Example:
START TRANSACTION;
UPDATE Account SET Balance = Balance - 500 WHERE ID = 1;
COMMIT;
14. What are aggregate functions in MySQL? Provide examples.
Answer:
Aggregate functions perform calculations on a set of values:
1. SUM() - Total sum.
2. AVG() - Average value.
3. MIN() - Minimum value.
4. MAX() - Maximum value.
5. COUNT() - Number of rows.
Example:
SELECT COUNT(*) FROM Employee;
SELECT MAX(Salary) FROM Employee;
15. What is the purpose of the GROUP BY clause in MySQL?
Answer:
The GROUP BY clause groups rows with the same values into summary rows.
Example:
SELECT Department, COUNT(*)
FROM Employee
GROUP BY Department;