GEO

SQL查询入门指南:从基础操作到实战应用

2026/1/23
SQL查询入门指南:从基础操作到实战应用
AI Summary (BLUF)

A query is a script used to request or modify data in a database, essential for programming and data analysis. This article explains SQL queries through practical examples, covering basic operations like SELECT, INSERT, UPDATE, and DELETE, and discusses their advantages and common commands. (查询是用于请求或修改数据库中数据的脚本,对编程和数据分析至关重要。本文通过实际示例解释SQL查询,涵盖SELECT、INSERT、UPDATE和DELETE等基本操作,并讨论其优势和常用命令。)

Introduction

Query is a fundamental yet critically important concept extensively used in programming and data analysis. This article will explain what a query is and how it is utilized in the daily work of a programmer or data analyst.

查询是编程和数据分析中一个基础但至关重要的概念,被广泛使用。本文将解释什么是查询,以及程序员或数据分析师在日常工作中如何运用它。

What is a Query?

When we begin our studies in programming and create our first applications, the need to persistently and securely store application and user information for long-term accessibility becomes imminent. To address this, we invest in implementing a database (e.g., PostgreSQL, MySQL, SQL Server, MongoDB, SQLite, etc.). Queries are the tools we use to manage this data.

当我们开始学习编程并创建第一个应用程序时,持久、安全地存储应用程序和用户信息以供长期访问的需求就变得迫在眉睫。为此,我们投资于实现一个数据库(例如 PostgreSQL、MySQL、SQL Server、MongoDB、SQLite 等)。查询是我们用来管理这些数据的工具。

It is a widespread, though potentially simplistic, notion that a query is merely about requesting information. However, we can understand this concept more broadly. A query is not just for retrieving data; it serves to manage the database as a whole. We can write queries to delete, insert, update, or retrieve information. Therefore, in programming or data analysis, a query is a script used to alter the state of a database or request the data stored within it.

一个普遍但可能过于简单的看法是,查询仅仅是请求信息。然而,我们可以更广泛地理解这个概念。查询不仅用于检索数据,它还用于整体管理数据库。我们可以编写查询来删除、插入、更新或检索信息。因此,在编程或数据分析中,查询是一个用于改变数据库状态或请求其中存储数据的脚本。

How Does a Query Work?

As mentioned, a query is a series of instructions that a Database Management System (DBMS) will use to perform a search or alter the state of stored data. This script is written using the syntax of the query language adopted by the DBMS, the most common being Structured Query Language (SQL).

如前所述,查询是一系列指令,数据库管理系统(DBMS)将使用这些指令来执行搜索或改变存储数据的状态。这个脚本是使用 DBMS 采用的查询语言语法编写的,最常见的是结构化查询语言(SQL)。

For example, suppose we have the following table named books in our database:

Title Author Genre Release Year
The Death of Ivan Ilyich Leo Tolstoy Fiction 1886
Crime and Punishment Fyodor Dostoevsky Novel 1866
The Hobbit J. R. R. Tolkien Fantasy 1937

例如,假设我们的数据库中有一个名为 books 的表,内容如下:

书名 作者 类型 出版年份
伊凡·伊里奇之死 列夫·托尔斯泰 小说 1886
罪与罚 费奥多尔·陀思妥耶夫斯基 长篇小说 1866
霍比特人 J. R. R. 托尔金 奇幻 1937

Now, let's assume we want to select all books of the 'Fiction' genre from our database. In SQL, we can write the following query:

SELECT * FROM books WHERE Genre = 'Fiction';

现在,假设我们想从数据库中选择所有类型为 '小说' 的书籍。在 SQL 中,我们可以编写以下查询

SELECT * FROM books WHERE Genre = '小说';

With this query, the DBMS should return all books where the value in the 'Genre' column equals 'Fiction'.

执行此查询,DBMS 应返回 '类型' 列中值等于 '小说' 的所有书籍。

Translating SQL Queries for Better Understanding

Let's translate the SQL query into a more didactic form:

  • Select (SELECT *) all books (FROM books) that are fiction (WHERE Genre = 'Fiction';).

让我们将 SQL 查询翻译成更易于理解的形式:

  • 选择 (SELECT *) 所有书籍 (FROM books) 中类型是小说 (WHERE Genre = '小说';) 的。

Notice that the query above is a sequence of reserved words that instruct the DBMS to perform a specific action on the database.

请注意,上面的查询是一系列保留字,用于指示 DBMS 对数据库执行特定操作。

Another example: let's now insert the book 'A Game of Thrones' into the table:

INSERT INTO books (Title, Author, Genre, Release_Year)
VALUES ('A Game of Thrones', 'George R. R. Martin', 'Fantasy', 1997);

另一个例子:现在让我们将《权力的游戏》这本书插入表中:

INSERT INTO books (Title, Author, Genre, Release_Year)
VALUES ('权力的游戏', '乔治·R·R·马丁', '奇幻', 1997);

Executing this query will insert a new row into the table containing the provided values. This is an example of how a query can serve to alter the state of the database. Our table would then look like this:

Title Author Genre Release Year
The Death of Ivan Ilyich Leo Tolstoy Fiction 1886
Crime and Punishment Fyodor Dostoevsky Novel 1866
The Hobbit J. R. R. Tolkien Fantasy 1937
A Game of Thrones George R. R. Martin Fantasy 1997

执行此查询将向表中插入包含所提供值的新行。这是一个查询如何用于改变数据库状态的例子。我们的表将变成这样:

书名 作者 类型 出版年份
伊凡·伊里奇之死 列夫·托尔斯泰 小说 1886
罪与罚 费奥多尔·陀思妥耶夫斯基 长篇小说 1866
霍比特人 J. R. R. 托尔金 奇幻 1937
权力的游戏 乔治·R·R·马丁 奇幻 1997

But what if we inserted incorrect data and want to change it? Simple, by executing an UPDATE. Let's see how this works:

UPDATE books SET Release_Year = '1996' WHERE Title = 'A Game of Thrones';

但是,如果我们插入了错误的数据并想更改它怎么办?很简单,执行一个 UPDATE。让我们看看这是如何工作的:

UPDATE books SET Release_Year = '1996' WHERE Title = '权力的游戏';

Executing this query will change the 'Release_Year' column for the book 'A Game of Thrones' from 1997 to 1996.

执行此查询会将《权力的游戏》这本书的 '出版年份' 列从 1997 年更改为 1996 年。

Finally, let's execute a query to delete a row from our database:

DELETE FROM books WHERE Title = 'Crime and Punishment';

最后,让我们执行一个查询来从数据库中删除一行:

DELETE FROM books WHERE Title = '罪与罚';

Executing this command will remove the book 'Crime and Punishment' from our table.

执行此命令将从我们的表中删除《罪与罚》这本书。

What Are the Advantages of Using SQL Queries?

Using queries allows for data retrieval in a simple and extremely flexible manner. It also enables data processing, making it more user-friendly and improving visualization. As demonstrated, queries also allow for the manipulation of your database data. The language is easy to learn in its basic principles because it features a syntax that allows for better and standardized comprehension.

使用查询可以以简单且极其灵活的方式检索数据。它还支持数据处理,使其更加用户友好并改善可视化效果。正如所演示的,查询还允许操作数据库数据。该语言的基本原理易于学习,因为它的语法允许更好和标准化的理解。

It is widely used across various DBMSs, with only minor changes, meaning you can leverage most of your knowledge to manage different types of databases.

它在各种 DBMS 中被广泛使用,只有微小的变化,这意味着您可以利用大部分知识来管理不同类型的数据库。

Due to its widespread use, you will find ample support and free or paid content available online to resolve your doubts and expand your knowledge.

由于其广泛使用,您可以在网上找到大量的支持和免费或付费内容来解决疑问并扩展知识。

What Are the Main SQL Query Commands?

These are the basic clauses of an SQL query:

  • CREATE TABLE: Creates a table in the database. (在数据库中创建表。)
  • DROP TABLE: Used to delete a table from the database. (用于从数据库中删除表。)
  • ALTER TABLE: Used to modify a column in a table. (用于修改表中的列。)
  • SELECT: Used to request data. (用于请求数据。)
  • UPDATE: Used to change the value of a column in the database. (用于更改数据库中某列的值。)
  • DELETE: Removes a row from a table in the database. (从数据库的表中删除一行。)
  • FROM: A sub-clause used to indicate the table on which the query will be executed. (用于指示将在哪个表上执行查询的子句。)
  • WHERE: A sub-clause used as a filter for the information to be returned/altered. (用作要返回/更改的信息的过滤器的子句。)
  • AND: An operator used to add conditions to the WHERE sub-clause and return values if they are true. (用于向 WHERE 子句添加条件并在条件为真时返回值的运算符。)
  • OR: An operator used to return a value if one of the conditions is true. (如果其中一个条件为真,则用于返回值的运算符。)
  • NOT: An operator used to return values that are not equal to the condition. (用于返回值不等于条件的运算符。)

With these commands, you can already create and manipulate tables in your database.

使用这些命令,您已经可以在数据库中创建和操作表。

Final Considerations and Recommendations for Using Queries

SQL query is a very powerful tool that will greatly assist in your data analysis and/or management, bringing you many benefits and results. As shown above, queries are relatively easy and friendly to learn, but the most important thing is to PRACTICE. So, how about putting into practice what you learned today?

SQL 查询是一个非常强大的工具,将极大地帮助您的数据分析和/或管理,为您带来许多好处和成果。如上所示,查询相对容易且友好易学,但最重要的是实践。那么,何不将今天所学付诸实践呢?

Learning more about queries will broaden your professional resume and increase your range of opportunities and tools. Today, it is a fundamental and mandatory requirement for a programmer, for example, to know about SQL queries and be able to query a database.

学习更多关于查询的知识将拓宽您的专业简历,增加您的机会和工具范围。例如,对于程序员来说,了解 SQL 查询并能够查询数据库是当今基本且强制性的要求。

Try using queries by creating your first database to store different types of information, such as product inventory for a store, information about students in a class, financial information for a bank, or anything else. Use creativity to practice the topic and see the use of queries in PRACTICE.

尝试通过创建您的第一个数据库来使用查询,以存储不同类型的信息,例如商店的产品库存、班级学生信息、银行的财务信息或其他任何信息。运用创造力来练习这个主题,并在实践中查看查询的使用。

Frequently Asked Questions (FAQs)

  1. What is a query? A query is a request for information from a database. (查询是什么?查询是向数据库请求信息。)
  2. How are queries used? They are used to retrieve, insert, update, or delete data from a database. (查询如何使用?它们用于从数据库中检索、插入、更新或删除数据。)
  3. Why is it important to understand queries? Understanding queries is essential for manipulating data and obtaining specific information from a database. (理解查询为什么重要?理解查询对于操作数据和从数据库获取特定信息至关重要。)
  4. What are the common types of queries? SELECT to retrieve data, INSERT to add, UPDATE to modify, and DELETE to delete records. (常见的查询类型有哪些?SELECT 用于检索数据,INSERT 用于添加,UPDATE 用于修改,DELETE 用于删除记录。)
  5. How to build an efficient query? Write clear queries, use appropriate filters, and understand the database structure. (如何构建高效的查询?编写清晰的查询,使用适当的过滤器,并理解数据库结构。)
  6. Is there a difference between SQL and NoSQL queries? Yes, SQL is used in relational databases, while NoSQL uses different approaches, such as document, key-value, etc. (SQL 和 NoSQL 查询有区别吗?是的,SQL 用于关系型数据库,而 NoSQL 使用不同的方法,例如文档、键值等。)
  7. What is query optimization? It is the process of adjusting a query to improve performance and execution efficiency. (什么是查询优化?这是调整查询以提高性能和执行效率的过程。)
  8. How to learn to create queries? There are online courses, tutorials, and specific documentation for learning to write queries in different languages. (如何学习创建查询?有在线课程、教程和特定文档可用于学习用不同语言编写查询。)
  9. Are queries exclusive to programmers? No, professionals from various fields, such as data analysis and system administration, can benefit from understanding and creating queries. (查询是程序员的专属吗?不是,数据分析、系统管理等各个领域的专业人士都可以从理解和创建查询中受益。)
  10. Where can I practice and improve my query skills? Online platforms, such as SQLFiddle and LeetCode, offer environments to practice and solve query-related challenges. (我可以在哪里练习和提高我的查询技能?SQLFiddle 和 LeetCode 等在线平台提供了练习和解决查询相关挑战的环境。)
← 返回文章列表
分享到:微博

版权与免责声明:本文仅用于信息分享与交流,不构成任何形式的法律、投资、医疗或其他专业建议,也不构成对任何结果的承诺或保证。

文中提及的商标、品牌、Logo、产品名称及相关图片/素材,其权利归各自合法权利人所有。本站内容可能基于公开资料整理,亦可能使用 AI 辅助生成或润色;我们尽力确保准确与合规,但不保证完整性、时效性与适用性,请读者自行甄别并以官方信息为准。

若本文内容或素材涉嫌侵权、隐私不当或存在错误,请相关权利人/当事人联系本站,我们将及时核实并采取删除、修正或下架等处理措施。 也请勿在评论或联系信息中提交身份证号、手机号、住址等个人敏感信息。