Explain what SQL is and its role in database management
Answer: SQL (Structured Query Language) is a language used to query and manage databases. It enables us to perform operations such as retrieving data, inserting, updating, and deleting data from a database. SQL is a fundamental tool for interacting with and manipulating data in most Database Management Systems (DBMS).
Trong SQL, SELECT, INSERT, UPDATE, DELETE
là những câu lệnh gì và chúng được sử dụng để làm gì?
Answer:
SELECT
: Retrieves data from a database to fetch information from one or more tables.INSERT
: Adds new data into a table in the database.UPDATE
: Modifies existing data in a table.DELETE
: Removes data from a table.
Explain the concepts of Primary Key
and Foreign Key
in SQL
Answer:
Primary Key
: It is a column or a set of columns used to uniquely identify each row in a table. It ensures uniqueness and identification for the data in the table.Foreign Key
: It is a column or a set of columns in one table that refers to the primary key of another table. It establishes a relationship between two tables in the database.
How to use the WHERE
clause in the SELECT
statement to filter data from a table
Answer: Use the WHERE
clause in the SELECT
statement to specify conditions that the rows must meet to be included in the query result.
For example:
SELECT * FROM Customers WHERE Country = 'USA';
How to use the JOIN
statement to combine data from multiple tables in SQL
Answer: The JOIN
statement is used to combine data from two or more tables based on a related column between them. There are different types of JOIN
, such as INNER JOIN, LEFT JOIN, RIGHT JOIN,FULL JOIN
.
For example:
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
Explain the usage of built-in functions in SQL like SUM, COUNT, AVG, MAX, MIN
Answer:
SUM
: Calculates the total value of a numeric column.COUNT
: Counts the number of rows in a table or the number of non-null values in a column.AVG
: Computes the average value of a numeric column.MAX
: Retrieves the maximum value in a column.MIN
: Retrieves the minimum value in a column.
How to use the GROUP BY
statement to group data in SQL
Answer: The GROUP BY
statement is used to group rows with the same values in one or more columns and perform aggregate functions on them.
For example:
SELECT Country, COUNT(*) AS TotalCustomers
FROM Customers
GROUP BY Country;
How to use the ORDER BY
statement to sort data in SQL
Answer: he ORDER BY statement is used to sort the query result based on one or more columns. The default is ascending order (ASC), but DESC can be used for descending order.
For example:
SELECT * FROM Customers ORDER BY FirstName ASC, LastName DESC;
How to use the INSERT INTO
statement to insert new data into a table
Answer: Use the INSERT INTO
statement to add new data to a table in the database
For example:
INSERT INTO Customers (CustomerName, ContactName, Country)
VALUES ('John Doe', 'John Doe Jr.', 'USA');
How to update data in a table using the UPDATE
statement in SQL.
Answer: Use the UPDATE
statement to modify existing data in a table.
For example:
UPDATE Customers
SET ContactName = 'Jane Smith'
WHERE CustomerID = 1;