locked
SQL Commands + Union Command RRS feed

  • Question

  • Hi all,

    I am new to this forms and commands too..

    1.Can any one provide me some usefull links for SQL Commands and books links...

    2.How to retrieve the data form any two tables that are stored in same database without union command?

    Waiting for REPLY...

    Thanks in advance..


    Pavan

    Saturday, March 17, 2012 5:01 AM

Answers

  • Hi Pavan,

    1. There are lot's of resources on the web for learning T-SQL. Check out SQL Server Books Online (BOL) on either

    • MSDN http://msdn.microsoft.com/en-us/library/ms130214.aspx,
    • Technet http://technet.microsoft.com/en-us/library/ms130214.aspx,
    • or in SQL Server Management Studio (SSMS) by pressing F1 on a key word. Al three are the same authorive manual of SQL Server.

    If your goal is to learn T-SQL for development, you could look at the recommended study material for the 70-433 TS: Microsoft SQL Server 2008, Database Development exam. http://www.microsoft.com/learning/en/us/exam.aspx?id=70-433#tab3

    If you however more interested in learning the command to administrate a server running SQL Server (i.e. more like everything outside the database) you could look at the recommended study material for the 70-432 TS: Microsoft SQL Server 2008, Implementation and Maintenance exam. http://www.microsoft.com/learning/en/us/exam.aspx?id=70-432#tab3

    If you are low on cash you could also consider buying SQL Server 2000 or 2005 books at a reduced, which are stil mostly useful for SQL Server 2008, R2, and 2012.

    Also please try out the script functionally in SSMS. Almost every dialog window in SSMS has a script button and almost every object in the Object Explorer can be scripted. You can study those scripts which to find out how all the command work.

    1. What is the reason you don't want to or can use the UNION command?
    • I assume both tables represent alike entities and  have the same or at least similar columns. An alternative to the UNION command is to create a temporary table or a table variable or perhaps (depending on your needs) even an actual normal table. (Please lookup and study the differences.) Insert the data in this new table. After that use a SELECT on the new table. Another way is to create a view which combines the two tables with the UNION command and then do a SELECT on this view. This is actually a pretend not to use UNION but it may help solve your problem. ;)
    • If the tables do not have the same columns, because they actually represent different entities. You should use JOIN, CROSS APPLY, sub SELECT's, and/or sub query's to combine the data from both tables.



    • Edited by Arjen Krap Saturday, March 17, 2012 4:29 PM
    • Proposed as answer by Naomi N Sunday, March 18, 2012 4:03 AM
    • Marked as answer by Kalman Toth Thursday, March 22, 2012 5:03 PM
    Saturday, March 17, 2012 4:27 PM

All replies

  • To retrieve data from two tables, you can follow the below ways - 

    1. If the data to be retrieved is not related, use two separate SELECT statements.
    2. If the data is related, you can make use of JOINS - INNER or OUTER or CROSS

    Please refer this link on JOINS - http://msdn.microsoft.com/en-us/library/ms191517.aspx and to lean more about TSQL, please follow this link - http://msdn.microsoft.com/en-us/library/bb264565(v=sql.90).aspx


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Saturday, March 17, 2012 5:14 AM
  • 1.Can any one provide me some usefull links for SQL Commands and books links...

    The Start menu. SQL Server. Documentation. Look for T-SQL Reference. (The exact place in the contents depends on your version of SQL Server.)


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
    Saturday, March 17, 2012 10:19 AM
  • Hi Pavan,

    1. There are lot's of resources on the web for learning T-SQL. Check out SQL Server Books Online (BOL) on either

    • MSDN http://msdn.microsoft.com/en-us/library/ms130214.aspx,
    • Technet http://technet.microsoft.com/en-us/library/ms130214.aspx,
    • or in SQL Server Management Studio (SSMS) by pressing F1 on a key word. Al three are the same authorive manual of SQL Server.

    If your goal is to learn T-SQL for development, you could look at the recommended study material for the 70-433 TS: Microsoft SQL Server 2008, Database Development exam. http://www.microsoft.com/learning/en/us/exam.aspx?id=70-433#tab3

    If you however more interested in learning the command to administrate a server running SQL Server (i.e. more like everything outside the database) you could look at the recommended study material for the 70-432 TS: Microsoft SQL Server 2008, Implementation and Maintenance exam. http://www.microsoft.com/learning/en/us/exam.aspx?id=70-432#tab3

    If you are low on cash you could also consider buying SQL Server 2000 or 2005 books at a reduced, which are stil mostly useful for SQL Server 2008, R2, and 2012.

    Also please try out the script functionally in SSMS. Almost every dialog window in SSMS has a script button and almost every object in the Object Explorer can be scripted. You can study those scripts which to find out how all the command work.

    1. What is the reason you don't want to or can use the UNION command?
    • I assume both tables represent alike entities and  have the same or at least similar columns. An alternative to the UNION command is to create a temporary table or a table variable or perhaps (depending on your needs) even an actual normal table. (Please lookup and study the differences.) Insert the data in this new table. After that use a SELECT on the new table. Another way is to create a view which combines the two tables with the UNION command and then do a SELECT on this view. This is actually a pretend not to use UNION but it may help solve your problem. ;)
    • If the tables do not have the same columns, because they actually represent different entities. You should use JOIN, CROSS APPLY, sub SELECT's, and/or sub query's to combine the data from both tables.



    • Edited by Arjen Krap Saturday, March 17, 2012 4:29 PM
    • Proposed as answer by Naomi N Sunday, March 18, 2012 4:03 AM
    • Marked as answer by Kalman Toth Thursday, March 22, 2012 5:03 PM
    Saturday, March 17, 2012 4:27 PM
  •  The following page has examples for INNER JOINs and OUTER JOINs:

    http://www.sqlusa.com/bestpractices/fourtablejoin/

    T-SQL programming examples link page:

    http://www.sqlusa.com/bestpractices


    Kalman Toth SQL SERVER & BI TRAINING

    Saturday, March 17, 2012 4:42 PM
  • --UNION-------------------- 
    
    SELECT Id FROM table1
    UNION
    SELECT Id FROM table2
    
    
    --UNION ALL-------------------------
    
    SELECT Id FROM Ad_table1
    UNION ALL
    SELECT Id FROM Ad_table2



    Kamal Saxena iBOOK dESK (UITS INDIA)

    Saturday, March 17, 2012 6:25 PM