Docsity

DATABASE NORMALIZATION + ERD REVIEWER, Exercises of Computer Programming

Toggle sidebar
bg1
MySQL SQL COMMANDS CHEAT SHEET
40 Questions with Answers
1. What is SQL?
SQL stands for Structured Query Language. It is used to manage and manipulate
relational databases.
2. What is the difference between SQL and MySQL?
SQL is a query language. MySQL is a relational database management system
(RDBMS) that uses SQL.
3. What is the SELECT command used for?
It retrieves data from a database table.
sql
SELECT * FROM users;
4. How do you select specific columns from a table?
sql
SELECT name, email FROM users;
5. What is the WHERE clause used for?
It filters records based on a condition.
sql
SELECT * FROM users WHERE age > 18;
6. What is the difference between WHERE and HAVING ?
WHERE filters rows before grouping. HAVING filters groups after GROUP BY .
7. What does ORDER BY do?
It sorts results in ascending ( ASC ) or descending ( DESC ) order.
sql
SELECT * FROM products ORDER BY price DESC;
pf3
pf4
pf5

Partial preview of the text

Download DATABASE NORMALIZATION + ERD REVIEWER and more Exercises Computer Programming in PDF only on Docsity!

MySQL SQL COMMANDS CHEAT SHEET

40 Questions with Answers

1. What is SQL?

SQL stands for Structured Query Language. It is used to manage and manipulate

relational databases.

2. What is the difference between SQL and MySQL?

SQL is a query language. MySQL is a relational database management system

(RDBMS) that uses SQL.

3. What is the SELECT command used for?

It retrieves data from a database table.

sql

SELECT * FROM users;

4. How do you select specific columns from a table?

sql

SELECT name, email FROM users;

5. What is the WHERE clause used for?

It filters records based on a condition.

sql

SELECT * FROM users WHERE age > 18;

6. What is the difference between WHERE and HAVING?

WHERE filters rows before grouping. HAVING filters groups after GROUP BY.

7. What does ORDER BY do?

It sorts results in ascending ( ASC ) or descending ( DESC ) order.

sql

SELECT * FROM products ORDER BY price DESC;

8. What is the GROUP BY clause used for?

It groups rows with the same values into summary rows.

sql

SELECT department, COUNT(*) FROM employees GROUP BY department;

9. What are aggregate functions in SQL?

Functions that perform calculations on

data: COUNT() , SUM() , AVG() , MIN() , MAX().

10. What does the DISTINCT keyword do?

It returns only unique values.

sql

SELECT DISTINCT city FROM customers;

11. What is the INSERT INTO command used for?

It inserts new records into a table.

sql

INSERT INTO users (name, email) VALUES ('John', '[email protected]');

12. How do you insert multiple records at once?

sql

INSERT INTO users (name, email) VALUES ('John', '[email protected]'), ('Jane', '[email protected]');

13. What is the UPDATE command used for?

It modifies existing records.

sql

UPDATE users SET email = '[email protected]' WHERE id = 1;

14. What is the DELETE command used for?

It removes records from a table.

22. What is an INNER JOIN?

Returns only matching rows from both tables.

sql

SELECT orders.id, users.name FROM orders INNER JOIN users ON orders.user_id = users.id;

23. What is a LEFT JOIN?

Returns all rows from the left table and matching rows from the right table. Non-matching

rows show NULL.

24. What is a RIGHT JOIN?

Returns all rows from the right table and matching rows from the left table.

25. What is a FULL OUTER JOIN?

Returns all rows from both tables, matching where possible. (Not supported in MySQL

directly.)

26. What is a UNION in SQL?

Combines results from two or more SELECT statements, removing duplicates.

sql

SELECT name FROM employees UNION SELECT name FROM contractors;

27. What is the difference between UNION and UNION ALL?

UNION removes duplicates. UNION ALL keeps all duplicates and is faster.

28. What is a subquery in SQL?

A query nested inside another query.

sql

SELECT name FROM users WHERE id IN (SELECT user_id FROM orders);

29. What is an index in MySQL?

An index speeds up data retrieval but slows down write operations.

sql

CREATE INDEX idx_name ON users (name);

30. What is a VIEW in MySQL?

A virtual table based on a SELECT query.

sql

CREATE VIEW active_users AS SELECT * FROM users WHERE status = 'active';

31. What is a stored procedure?

A precompiled set of SQL statements stored on the server.

sql

DELIMITER // CREATE PROCEDURE GetUsers() BEGIN SELECT * FROM users; END // DELIMITER ;

32. What is a TRIGGER in MySQL?

An automated action that runs before or after an INSERT , UPDATE , or DELETE.

33. What is a TRANSACTION in MySQL?

A group of SQL statements executed as a single unit.

Supports COMMIT and ROLLBACK.

34. What are ACID properties in databases?

Atomicity, Consistency, Isolation, Durability.

35. What is the LIKE operator used for?

Pattern matching with wildcards ( % and _ ).

sql

SELECT * FROM users WHERE name LIKE 'J%';

36. What is the IN operator used for?

Checks if a value matches any value in a list.

sql

SELECT * FROM users WHERE id IN (1, 2, 3);