![]() The result set includes five rows in the result set because the UNION operator removes one duplicate row. UNION SELECT * FROM most_popular_films Code language: SQL (Structured Query Language) ( sql ) The following statement uses the UNION operator to combine data from both tables: SELECT * FROM top_rated_films Let’s take some examples of using the PostgreSQL UNION operator. The following statement returns the data from the most_popular_films table: SELECT * FROM most_popular_films Code language: SQL (Structured Query Language) ( sql ) PostgreSQL UNION examples The following shows the data from the top_rated_films table: SELECT * FROM top_rated_films Code language: SQL (Structured Query Language) ( sql ) ( 'Greyhound', 2020) Code language: SQL (Structured Query Language) ( sql ) The following statements create two tables: top_rated_films and most_popular_films, and insert data into these tables: DROP TABLE IF EXISTS top_rated_films In practice, you often use the UNION operator to combine data from similar tables, which are not perfectly normalized, in the data warehouse or business intelligence systems. ![]() To sort rows in the final result set, you use the ORDER BY clause in the second query. The UNION operator may place the rows from the result set of the first query before, after, or between the rows from the result set of the second query. The following Venn digram illustrates how to the UNION works: PostgreSQL UNION with ORDER BY clause To retain the duplicate rows, you use the the UNION ALL instead. The UNION operator removes all duplicate rows from the combined data set. The number and the order of the columns in the select list of both queries must be the same.To combine the result sets of two queries using the UNION operator, the queries must conform to the following rules: The following illustrates the syntax of the UNION operator that combines result sets from two queries: SELECT select_list_1įROM table_expression_2 Code language: SQL (Structured Query Language) ( sql ) The UNION operator combines result sets of two or more SELECT statements into a single result set. Introduction to PostgreSQL UNION operator Summary: in this tutorial, you will learn how to use PostgreSQL UNION operator to combine result sets of multiple queries into a single result sets.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |