【SQL】SQL 高频面试题英语版(1)

今天更新 SQL 高频面试题英语版。音频点击下方获取。
【SQL】SQL 高频面试题英语版(1)
【SQL】SQL 高频面试题英语版(2)

SQL Interview Questions

1. What is Database?

A database is an organized collection of data, stored and retrieved digitally from a remote or local computer system. Databases can be vast and complex, and such databases are developed using fixed design and modeling approaches.

1. 什么是数据库?

数据库是有组织的数据集合,从远程或本地计算机系统以数字方式存储和检索。数据库可能庞大而复杂,并且此类数据库是使用固定设计和建模方法开发的。

2. What is DBMS?

DBMS stands for Database Management System. DBMS is a system software responsible for the creation, retrieval, updation, and management of the database. It ensures that our data is consistent, organized, and is easily accessible by serving as an interface between the database and its end-users or application software.

2. 什么是 DBMS?

DBMS 代表数据库管理系统。DBMS 是一个系统软件,负责数据库的创建、检索、更新和管理。它通过充当数据库与其最终用户或应用程序软件之间的接口,确保我们的数据是一致的、有组织的并且易于访问。

3. What is RDBMS? How is it different from DBMS?

RDBMS stands for Relational Database Management System. The key difference here, compared to DBMS, is that RDBMS stores data in the form of a collection of tables, and relations can be defined between the common fields of these tables. Most modern database management systems like MySQL, Microsoft SQL Server, Oracle, IBM DB2, and Amazon Redshift are based on RDBMS.

3. 什么是关系型数据库?它与 DBMS 有何不同?

RDBMS 代表关系数据库管理系统。与 DBMS 相比,这里的关键区别在于 RDBMS 以表集合的形式存储数据,并且可以在这些表的公共字段之间定义关系。大多数现代数据库管理系统(如 MySQL、Microsoft SQL Server、Oracle、IBM DB2 和 Amazon Redshift)都基于 RDBMS。

4. What is SQL?

SQL stands for Structured Query Language. It is the standard language for relational database management systems. It is especially useful in handling organized data comprised of entities (variables) and relations between different entities of the data.

4. 什么是 SQL?

SQL 代表结构化查询语言。它是关系数据库管理系统的标准语言。它在处理由实体(变量)和数据的不同实体之间的关系组成的有组织的数据时特别有用。

5. What is the difference between SQL and MySQL?

SQL is a standard language for retrieving and manipulating structured databases. On the contrary, MySQL is a relational database management system, like SQL Server, Oracle or IBM DB2, that is used to manage SQL databases.

5. SQL 和 MySQL 有什么区别?

SQL 是用于检索和操作结构化数据库的标准语言。相反,MySQL 是一个关系数据库管理系统,如 SQL Server、Oracle 或 IBM DB2,用于管理 SQL 数据库。

6. What are Tables and Fields?

A table is an organized collection of data stored in the form of rows and columns. Columns can be categorized as vertical and rows as horizontal. The columns in a table are called fields while the rows can be referred to as records.

6. 什么是表和字段?

表是以行和列的形式存储的有组织的数据集合。列可以分类为垂直,行可以分类为水平。表中的列称为字段,而行可以称为记录。

7. What are Constraints in SQL?

Constraints are used to specify the rules concerning data in the table. It can be applied for single or multiple fields in an SQL table during the creation of the table or after creating using the ALTER TABLE command. The constraints are:

  • NOT NULL - Restricts NULL value from being inserted into a column.
  • CHECK - Verifies that all values in a field satisfy a condition.
  • DEFAULT - Automatically assigns a default value if no value has been specified for the field.
  • UNIQUE - Ensures unique values to be inserted into the field.
  • INDEX - Indexes a field providing faster retrieval of records.
  • PRIMARY KEY - Uniquely identifies each record in a table.
  • FOREIGN KEY - Ensures referential integrity for a record in another table.

7. SQL 中的约束是什么?

约束用于指定有关表中数据的规则。它可以在创建表期间或使用 ALTER TABLE 命令创建后应用于 SQL 表中的单个或多个字段。约束是:

  • NOT NULL - 限制将 NULL 值插入列中。
  • CHECK - 验证字段中的所有值是否满足条件。
  • DEFAULT - 如果没有为该字段指定值,则自动分配默认值。
  • UNIQUE - 确保将唯一值插入到字段中。
  • INDEX - 索引一个提供更快检索记录的字段。
  • PRIMARY KEY - 唯一标识表中的每条记录。
  • FOREIGN KEY - 确保另一个表中记录的引用完整性。

8. What is a Primary Key?

The PRIMARY KEY constraint uniquely identifies each row in a table. It must contain UNIQUE values and has an implicit NOT NULL constraint.
A table in SQL is strictly restricted to have one and only one primary key, which is comprised of single or multiple fields (columns).

8. 什么是主键?

PRIMARY KEY 约束唯一标识表中的每一行。它必须包含 UNIQUE 值并具有隐式 NOT NULL 约束。
SQL 中的表被严格限制为只有一个主键,主键由单个或多个字段(列)组成。

CREATE TABLE Students (   /* Create table with a single field as primary key */
   ID INT NOT NULL
   Name VARCHAR(255)
   PRIMARY KEY (ID)
);

CREATE TABLE Students (   /* Create table with multiple fields as primary key */
   ID INT NOT NULL
   LastName VARCHAR(255)
   FirstName VARCHAR(255) NOT NULL,
   CONSTRAINT PK_Student
   PRIMARY KEY (ID, FirstName)
);

ALTER TABLE Students   /* Set a column as primary key */
ADD PRIMARY KEY (ID);
ALTER TABLE Students   /* Set multiple columns as primary key */
ADD CONSTRAINT PK_Student   /*Naming a Primary Key*/
PRIMARY KEY (ID, FirstName);

9. What is a UNIQUE constraint?

A UNIQUE constraint ensures that all values in a column are different. This provides uniqueness for the column(s) and helps identify each row uniquely. Unlike primary key, there can be multiple unique constraints defined per table. The code syntax for UNIQUE is quite similar to that of PRIMARY KEY and can be used interchangeably.

9. 什么是 UNIQUE 约束?

UNIQUE 约束确保列中的所有值都不同。这为列提供了唯一性,并有助于唯一地标识每一行。与主键不同,每个表可以定义多个唯一约束。UNIQUE 的代码语法与 PRIMARY KEY 的代码语法非常相似,可以互换使用。

CREATE TABLE Students (   /* Create table with a single field as unique */
   ID INT NOT NULL UNIQUE
   Name VARCHAR(255)
);

CREATE TABLE Students (   /* Create table with multiple fields as unique */
   ID INT NOT NULL
   LastName VARCHAR(255)
   FirstName VARCHAR(255) NOT NULL
   CONSTRAINT PK_Student
   UNIQUE (ID, FirstName)
);

ALTER TABLE Students   /* Set a column as unique */
ADD UNIQUE (ID);
ALTER TABLE Students   /* Set multiple columns as unique */
ADD CONSTRAINT PK_Student   /* Naming a unique constraint */
UNIQUE (ID, FirstName);

10. What is a Foreign Key?

A FOREIGN KEY comprises of single or collection of fields in a table that essentially refers to the PRIMARY KEY in another table. Foreign key constraint ensures referential integrity in the relation between two tables.
The table with the foreign key constraint is labeled as the child table, and the table containing the candidate key is labeled as the referenced or parent table.

10. 什么是外键?

FOREIGN KEY 由表中的单个字段或字段集合组成,这些字段本质上是指另一个表中的 PRIMARY KEY。外键约束确保两个表之间关系的参照完整性。
具有外键约束的表被标记为子表,包含候选键的表被标记为引用表或父表。

CREATE TABLE Students (   /* Create table with foreign key - Way 1 */
   ID INT NOT NULL
   Name VARCHAR(255)
   LibraryID INT
   PRIMARY KEY (ID)
   FOREIGN KEY (Library_ID) REFERENCES Library(LibraryID)
);

CREATE TABLE Students (   /* Create table with foreign key - Way 2 */
   ID INT NOT NULL PRIMARY KEY
   Name VARCHAR(255)
   LibraryID INT FOREIGN KEY (Library_ID) REFERENCES Library(LibraryID)
);

ALTER TABLE Students   /* Add a new foreign key */
ADD FOREIGN KEY (LibraryID)
REFERENCES Library (LibraryID);

11. What is a Join? List its different types.

The SQL Join clause is used to combine records (rows) from two or more tables in a SQL database based on a related column between the two.

11. 什么是 Join?列出它的不同类型。

SQL Join 子句用于根据两者之间的相关列组合来自 SQL 数据库中两个或多个表的记录(行)。

There are four different types of JOINs in SQL:

  • (INNER) JOIN: Retrieves records that have matching values in both tables involved in the join. This is the widely used join for queries.
  • LEFT (OUTER) JOIN: Retrieves all the records/rows from the left and the matched records/rows from the right table.
  • RIGHT (OUTER) JOIN: Retrieves all the records/rows from the right and the matched records/rows from the left table.
  • FULL (OUTER) JOIN: Retrieves all the records where there is a match in either the left or right table.

SQL 中有四种不同类型的 JOIN:

  • (INNER) JOIN: 检索在连接所涉及的两个表中具有匹配值的记录。这是广泛使用的查询连接。
  • LEFT (OUTER) JOIN: 从左侧检索所有记录/行,从右侧表中检索匹配的记录/行。
  • RIGHT (OUTER) JOIN: 从右侧检索所有记录/行,从左侧表中检索匹配的记录/行。
  • FULL (OUTER) JOIN: 检索左表或右表中匹配的所有记录。

12. What is a Self-Join?

A self JOIN is a case of regular join where a table is joined to itself based on some relation between its own column(s). Self-join uses the INNER JOIN or LEFT JOIN clause and a table alias is used to assign different names to the table within the query.

12. 什么是自联接?

联接是常规联接的一种情况,其中表根据其自身列之间的某种关系连接到自身。自联接使用 INNER JOIN 或 LEFT JOIN 子句,表别名用于为查询中的表分配不同的名称。

13. What is a Cross-Join?

Cross join can be defined as a cartesian product of the two tables included in the join. The table after join contains the same number of rows as in the cross-product of the number of rows in the two tables. If a WHERE clause is used in cross join then the query will work like an INNER JOIN.

13. 什么是交叉连接?

交叉连接可以定义为连接中包含的两个表的笛卡尔积。连接后的表包含与两个表中行数的叉积相同的行数。如果在交叉连接中使用 WHERE 子句,则查询将像 INNER JOIN 一样工作。

14. What is an Index? Explain its different types.

A database index is a data structure that provides a quick lookup of data in a column or columns of a table. It enhances the speed of operations accessing data from a database table at the cost of additional writes and memory to maintain the index data structure.

14. 什么是索引?解释它的不同类型。

数据库索引是一种数据结构,可以快速查找表的一列或多列中的数据。它提高了从数据库表访问数据的操作速度,代价是额外的写入和内存以维护索引数据结构。

CREATE INDEX index_name   /* Create Index */
ON table_name (column_1, column_2);
DROP INDEX index_name;   /* Drop Index */

There are different types of indexes that can be created for different purposes:

可以为不同目的创建不同类型的索引:

  • Unique and Non-Unique Index:
  • 唯一和非唯一索引:

Unique indexes are indexes that help maintain data integrity by ensuring that no two rows of data in a table have identical key values. Once a unique index has been defined for a table, uniqueness is enforced whenever keys are added or changed within the index.

唯一索引是通过确保表中没有两行数据具有相同键值来帮助维护数据完整性的索引。一旦为表定义了唯一索引,只要在索引中添加或更改键,就会强制执行唯一性。

CREATE UNIQUE INDEX myIndex
ON students (enroll_no);

Non-unique indexes, on the other hand, are not used to enforce constraints on the tables with which they are associated. Instead, non-unique indexes are used solely to improve query performance by maintaining a sorted order of data values that are used frequently.

另一方面,非唯一索引不用于对与其关联的表实施约束。相反,非唯一索引仅用于通过维护经常使用的数据值的排序顺序来提高查询性能。

  • Clustered and Non-Clustered Index:
  • 聚集和非聚集索引:

Clustered indexes are indexes whose order of the rows in the database corresponds to the order of the rows in the index. This is why only one clustered index can exist in a given table, whereas, multiple non-clustered indexes can exist in the table.

The only difference between clustered and non-clustered indexes is that the database manager attempts to keep the data in the database in the same order as the corresponding keys appear in the clustered index.

Clustering indexes can improve the performance of most query operations because they provide a linear-access path to data stored in the database.

聚集索引是数据库中行的顺序与索引中的行的顺序相对应的索引。这就是为什么给定的表中只能存在一个聚集索引,而表中可以存在多个非聚集索引。

聚簇索引和非聚簇索引之间的唯一区别是数据库管理器尝试将数据库中的数据保持在与相应键出现在聚簇索引中相同的顺序。

集群索引可以提高大多数查询操作的性能,因为它们为存储在数据库中的数据提供了线性访问路径。

15. What is the difference between Clustered and Non-clustered index?

As explained above, the differences can be broken down into three small factors -

  • Clustered index modifies the way records are stored in a database based on the indexed column. A non-clustered index creates a separate entity within the table which references the original table.
  • Clustered index is used for easy and speedy retrieval of data from the database, whereas, fetching records from the non-clustered index is relatively slower.
  • In SQL, a table can have a single clustered index whereas it can have multiple non-clustered indexes.

15. 聚簇索引和非聚簇索引有什么区别?

如上所述,差异可以分为三个小因素 -

  • 聚集索引根据索引列修改记录在数据库中的存储方式。非聚集索引在表中创建一个引用原始表的单独实体。
  • 聚集索引用于从数据库中轻松快速地检索数据,而从非聚集索引中获取记录相对较慢。
  • 在 SQL 中,一个表可以有一个聚集索引,而它可以有多个非聚集索引。

16. What is Data Integrity?

Data Integrity is the assurance of accuracy and consistency of data over its entire life-cycle and is a critical aspect of the design, implementation, and usage of any system which stores, processes, or retrieves data. It also defines integrity constraints to enforce business rules on the data when it is entered into an application or a database.

16. 什么是数据完整性?

数据完整性是数据在其整个生命周期内的准确性和一致性的保证,并且是任何存储、处理或检索数据的系统的设计、实施和使用的关键方面。它还定义了完整性约束,以便在将数据输入应用程序或数据库时对数据实施业务规则。

17. What is a Query?

A query is a request for data or information from a database table or combination of tables. A database query can be either a select query or an action query.

17. 什么是查询?

查询是对来自数据库表或表组合的数据或信息的请求。数据库查询可以是选择查询或操作查询。

18. What is a Subquery? What are its types?

A subquery is a query within another query, also known as a nested query or inner query. It is used to restrict or enhance the data to be queried by the main query, thus restricting or enhancing the output of the main query respectively. For example, here we fetch the contact information for students who have enrolled for the maths subject:

18. 什么是子查询?它有哪些类型?

子查询是另一个查询中的查询,也称为嵌套查询内部查询。用于限制或增强主查询要查询的数据,从而分别限制或增强主查询的输出。例如,在这里我们获取注册数学科目的学生的联系信息:

SELECT name, email, mob, address
FROM myDb.contacts
WHERE roll_no IN (
 SELECT roll_no
 FROM myDb.students
 WHERE subject = 'Maths');

There are two types of subquery - Correlated and Non-Correlated.

  • A correlated subquery cannot be considered as an independent query, but it can refer to the column in a table listed in the FROM of the main query.
  • A non-correlated subquery can be considered as an independent query and the output of the subquery is substituted in the main query.

有两种类型的子查询 - 关联非关联

  • 关联子查询不能视为独立查询,但可以引用主查询的 FROM 中列出的表中的列。
  • 一个非关联子查询可以被认为是一个独立的查询,并且子查询的输出被替换在主查询中。
-- 关联子查询
select d.*
from dept d
where exists (
    select 1
    from emp e
    where e.deptno = d.deptno
); 

-- 非关联子查询
select d.*
from dept d
where d.deptno = (select max(e.deptno) from emp e); 

19. What is the SELECT statement?

SELECT operator in SQL is used to select data from a database. The data returned is stored in a result table, called the result-set.

19. 什么是SELECT语句?

SQL 中的 SELECT 运算符用于从数据库中选择数据。返回的数据存储在结果表中,称为结果集。

20. What are some common clauses used with SELECT query in SQL?

Some common SQL clauses used in conjuction with a SELECT query are as follows:

  • WHERE clause in SQL is used to filter records that are necessary, based on specific conditions.
  • ORDER BY clause in SQL is used to sort the records based on some field(s) in ascending (ASC) or descending order (DESC).

20、SQL 中 SELECT 查询常用的子句有哪些?

与 SELECT 查询结合使用的一些常见 SQL 子句如下:

  • SQL 中的 WHERE 子句用于根据特定条件过滤必要的记录。
  • SQL 中的 ORDER BY 子句用于根据某些字段按升序 (ASC) 或降序 (DESC) 对记录进行排序。
SELECT *
FROM myDB.students
WHERE graduation_year = 2019
ORDER BY studentID DESC;
  • GROUP BY clause in SQL is used to group records with identical data and can be used in conjunction with some aggregation functions to produce summarized results from the database.
  • HAVING clause in SQL is used to filter records in combination with the GROUP BY clause. It is different from WHERE, since the WHERE clause cannot filter aggregated records.
  • SQL 中的 GROUP BY 子句用于对具有相同数据的记录进行分组,并且可以与一些聚合函数结合使用以从数据库中生成汇总结果。
  • SQL 中的 HAVING 子句结合 GROUP BY 子句用于过滤记录。它与 WHERE 不同,因为 WHERE 子句不能过滤聚合记录。
SELECT COUNT(studentId), country
FROM myDB.students
WHERE country != "INDIA"
GROUP BY country
HAVING COUNT(studentID) > 5;

猜你喜欢

转载自blog.csdn.net/weixin_45545090/article/details/125652804