- Learning PostgreSQL 11
- Salahaldin Juba Andrey Volkov
- 567字
- 2021-07-02 13:11:39
The SELECT and PROJECT operations
SELECT is used to restrict tuples from the relation. SELECT always returns a unique set of tuples; that is inherited from entity integrity constraint. For example, the query give me the customer information where the customer_id equals 2 is written as follows:
The selection, as mentioned earlier, is commutative; the query give me all customers where the customer's email is known, and the customer's first name is kim is written in three different ways, as follows:
The selection predicates are certainly determined by the data types. For numeric data types, the comparison operator might be ≠, =, <, >, ≥, or ≤. The predicate expression can also contain complex expressions and functions. The equivalent SQL statement for the SELECT operator is the SELECT * statement, and the predicate is defined in the WHERE clause.
The following SELECT statement is equivalent to the relational algebra expression σcustomer_id =2 customer:
SELECT * FROM customer WHERE customer_id = 2;
The PROJECT operation could be visualized as a vertical slicing of the table. The query give me the customer names is written in relational algebra as follows:
The following is the result of projection expression:

Duplicate tuples are not allowed in the formal relational model; the number of tuples returned from the PROJECT operator is always equal to or less than the number of total tuples in the relation. If a PROJECT operator's attribute list contains a primary key, then the resultant relation has the same number of tuples as the projected relation.
The projection operator also can be optimized, for example, cascading projections could be optimized as the following expression:
The SQL equivalent for the PROJECT operator is SELECT DISTINCT. The DISTINCT keyword is used to eliminate duplicates. To get the result shown in the preceding expression, one could execute the following SQL statement:
SELECT DISTINCT first_name, last_name FROM customers;
The sequence of the execution of the PROJECT and SELECT operations can be interchangeable in some cases. The query give me the name of the customer with customer_id equal to 2 could be written as follows:
In other cases, the PROJECT and SELECT operators must have an explicit order, as shown in the following example; otherwise, it will lead to an incorrect expression. The query give me the last name of the customers where the first name is kim could be written in the following way:
- Go Web編程
- ClickHouse性能之巔:從架構(gòu)設(shè)計解讀性能之謎
- MySQL 8 DBA基礎(chǔ)教程
- 算法精粹:經(jīng)典計算機科學(xué)問題的Python實現(xiàn)
- Hands-On JavaScript High Performance
- 硅谷Python工程師面試指南:數(shù)據(jù)結(jié)構(gòu)、算法與系統(tǒng)設(shè)計
- R語言與網(wǎng)絡(luò)輿情處理
- C/C++程序員面試指南
- INSTANT Adobe Edge Inspect Starter
- HTML+CSS+JavaScript編程入門指南(全2冊)
- Java并發(fā)編程:核心方法與框架
- Visual Basic程序設(shè)計全程指南
- Visual C++開發(fā)寶典
- 從零開始學(xué)UI:概念解析、實戰(zhàn)提高、突破規(guī)則
- Learning iOS Penetration Testing