Union


There may be occasions where there is a need to merge data from two or more tables and rename the column names in the output.

One such business scenario which may arise, is if there is a need to send an email broadcast to both customers and staff alike. This could be an announcement that you would like to share with your customers, but you need to copy in to your sales staff so that they can also read the message being sent to customers.

This can be achieved by using the UNION function, which can merge the output from two or more queries.

Below is the result of this live query:


salutationfirstnamesurnameemail
MrsAntheaAdamsatraders@hotmailtest.com
MrBertieBillinghambertie@billinghamstest.co.uk
MrCecilCollinscolcon@gmailtest.com
MissDianaDimblebydimblebyplc@talktalktest.co.uk
MsElaineEastsales@eastempiretest.com
MrFrederickFranklinfred@frannockstest.com
MrGeorgeGimleygeorgegimley@aoltest.com
MissHannahHamshawhh@hamshawltdtest.com
MrsIreneIngolsiingols@gmailtest.com
MrJamieJedsonjamiej@jedsonstest.com
MrKenKendrickkkendrick@kimboltoncarrierstest.co.uk
MrLiamLeellee@lemmystest.com
MissMirandaMulgroonmiranda@mulgroonstest.co.uk
MrsNormaNixonnnixon@normascakestest.com
MrOliverOswaldosfurnishings@yahootest.co.uk
MrPeterPeacockpeterp@hotmailtest.com
MrQuentinQuinnqquinn@talktalktest.co.uk
MrsRenaRimshawrrimshaw@renastest.com
MrSimonSixsmithsimon.sixsmith@shenleysshovelstest.co.uk
MrTimTimpkinst.timpkins@tmltdtest.co.uk
MsUnaUnsworthuunsworth@aoltest.com
MrVinnieVengavinnie.venga@yahootest.co.uk
MrWillWiltonwill@wiltonstest.com
MrsXenaXavierxena@xavierctoctest.com
MissYasmineYolanderyasmine@yaxleypiestest.co.uk
MrZakZanettizakzanetti@hotmailtest.com
MrArnoldAmbletonaa@hotmailtest.com
MrBertieBeckleybb@beckleytest.com
MrCedricColliercedric@collierstest.com
MrDerekDuketheboss@dukestest.co.uk
MrEddieEmmersoneddie@emmersonstest.com
MissFionaFinleyff@fenthamfarmstest.net
MrGarryGumtongary@gurnockstest.com
MrHenryHammertonhenry@hammertontest.co.uk
MrIanIckeii@irlingtonltdtest.com
MrJeffJohnstonmrjohnston@johnstontest.com
MrsKarenKensomekk@kandktest.com
MrsLenaLillylenalilly@locallandmarkstest.com
MrMatthewMarshmatt@motormantest.com
MsNoreenNightingalenn@noreennightingaletest.com
MrOrvilleOwenorville@oakham-optometriststest.com
MrPaulPurvispp@purvisandpurvistest.com
MrQuincyQuantquincy@quakermanstest.com
MrRogerRenleyroger@renleystest.com
MrsSallySmitherssally@smitherstest.com
MrTobyThompsontthompson@hotmailtest.com
MrsUrsulaUmbridgesales@umbridgetest.com
MrsVioletVardyvv@violetvanstest.net
MrWayneWilliamswayne@wwtest.co.uk
MrsXariaXandaxx@xceltest.com
MrYusefYorkeyusef@yeovilyardstest.com
MrZaneZellwegerzane@zenstest.com
MrsAntheaAllena.allen@widge-it.com
MrBillyBeaumontb.beaumont@widge-it.com
MrCharlesChuzzlewitc.chuzzlewit@widge-it.com
MissDeirdrieDawsond.dawson@widge-it.com
MrErnieEcclestonee.ecclestone@widge-it.com
MrsFrancescaFavershamf.faversham@widge-it.com
MissGinaGinolag.ginola@widge-it.com
MrHarryHarrisonh.harrison@widge-it.com
Code for both MySQL and T-SQL (MS SQL)

SELECT cust_salutation AS salutation ,cust_firstname AS firstname, cust_surname AS surname, cust_email AS email
FROM customer
UNION
SELECT staff_salutation, staff_firstname, staff_surname, staff_email
FROM staff


Click the button below to advance to the next stage


Next: Nested IFs


Or use the above 'SQL Queries' dropdown menu to jump to any stage.