JOINS in SQL are commands which are used to combine rows from two or more tables, based on a related column between those tables. Moving forward in this article, let us understand how to use the UNION and UNION ALL clauses with JOINS. Write a query to retrieve Indian cities and their postal codes from both the Employees and Projects table, where duplicate values are allowed SELECT City, PostalCode, Country FROM Projects SELECT City, PostalCode, Country FROM Employees Write a query to retrieve the distinct Indian cities and their postal codes from both the Employees and Projects table. SELECT 'Employee' AS Type, Name, City, Country So, let us write a query to list all the unique employees and projects. SQL Aliases are used to give a table or a column a temporary name. Next in this article, let us understand how to use the UNION clause with SQL Aliases. Write a query to retrieve cities from the Employees and Projects table. Write a query to retrieve distinct cities from the Employees and Projects table. SQL UNION Examples UNION Operator Example Examples of SQL UNION and UNION ALLįor your better understanding, I will be considering the following tables to show you different examples. Next, in this article on SQL UNION, let us understand the different ways to use this operator. Moving on in this article, let us understand the differences between UNION and UNION ALL.ĭifferences between SQL UNION and UNION ALLĬombines two or more result set and does not keep the duplicate values.Ĭombines two or more result set and keeps the duplicate values. Moving on in this article on SQL UNION, let us understand the syntax. Just in case, you want duplicate values, then you have to use the UNION ALL clause. The UNION clause gives only unique values as output. Also, all the columns present in the SELECT statements must have similar data types. Here each SELECT statement used within the UNION statement must have the same number of columns in the same order. Let us get started! What is the SQL UNION operator?Īs the name suggests, this operator/ clause is used to combine the results of two or more SELECT statements.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |