Software Testing

k2G9Eo9
RdZQRt6

Popular Posts

UNION

UNION

The SQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements. Each SELECT statement within the UNION must have the same number of fields in the result sets with similar data types.
The basic syntax is as follows:
SELECT Column1, column2, ... column_n 
FROM tables
WHERE conditions 
UNION 
SELECT
 Column1, column2, ... column_n 
FROM tables 
WHERE conditions;

Parameters or Arguments

  • aggregate_function
    • A function such as SUM, COUNTMINMAX, or AVG functions.
  • column1, column2, ... column_n
    • The columns that are not encapsulated within an aggregate function and must be included in the GROUP BY clause.
  • condition
    • The condition that is used to restrict the groups of returned rows. Only those groups whose condition evaluates to TRUE will be included in the result set.

    Employee
    Emp_idNameSurnameSalaryDept_id
    1RajeshKhanna450002
    2SnehaGupta550004
    3SandeepNehte950001
    4KirtiPatil250003
    5NileshJadhav450003
    6DipeshDas350005
    7KailashRane620001
    8KavitaDeshpande480003
    9SanjayDatt500003
    10ShrutiHasan780001
    Employee
    Dept_idDept_NameLoc_id
    1IT101
    2EC108
    3SUPPORT103
    4MANAGEMENT104
    5CIVIL105
    Syntax For MySQL:
    SELECT Dept_id 
    FROM Employee
    UNION 
    SELECT Dept_id 
    FROM Department 
    After Execute: 
    Dept_id
    2
    4
    1
    3
    8
    0
    5

    UNION ALL

    The UNION ALL operator is used to combine the results of two SELECT statements including duplicate rows. Each SELECT statement within the UNION ALL must have the same number of fields in the result sets with similar data types.
    The basic syntax is as follows:
    SELECT Column1, column2, ... column_n 
    FROM tables
    WHERE conditions 
    UNION ALL
    SELECT
     Column1, column2, ... column_n 
    FROM tables 
    WHERE conditions;
    SQL syntax:
    SELECT Dept_id 
    FROM Employee
    WHERE Salary > 60000 
    UNION ALL
    SELECT
     Dept_id 
    FROM Department 
    WHERE Dept_Name ='Sandeep';
    After Execute: 
    Dept_id
    1
    1
    1

    No comments:

    Post a Comment