SQL SERVER PART II

 Hey Guys, I hope that you are enjoying with my blogs writing and today's we will cover some  "SQL SERVER" Concepts.


Microsoft SQL Server is a relational database management system, or RDBMS, developed and marketed by Microsoft.
As a database server, it is a software products with primary function of storing and retrieving data as requested by other software application.
Initial Releases is April 24, 1989;

   SQL UNION OPERATOR:
  • The union operator is used to combine the result-set of two or more select statements.
  • Every select statements within union have the same number of columns
  • The columns also have similar data type
  • The columns in every select statement must also be in the same order
                         
  • Syntax: 
  • For Distinct Contents: 
        select column_name(s) from table1 union select column(s) from table1;
        For Example: 
                              Input:
    select Id, FirstName, LastName from tblCustomer1


union
select Id, FirstName, LastName from tblexpired



                                Output:


  • For All Contents:
        select column_name(s) from table1 union select column(s) from table1;

          For Example: Input
 select Id, FirstName, LastName from tblCustomer1
union all
select Id, FirstName, LastName from tblexpired
                                Output: Both table has mentioned above and we have taken UNION ALL.
                             Note: We can indicate content by using as normal and expired or anything like:

select 'nomal' as type1, Id, FirstName, LastName from tblCustomer1
union all
select 'expired' as type1,Id, FirstName, LastName from tblexpired

  • GROUP BY:
  •  This statement is often used with aggregate function(COUNT(),MAX(), MIN(),SUM,AVG()).
  • For Example: we have created a table "tblCustomer_ctry" by using this aggregate function we will find its sum of sales of two Countries.
  • "tblCustomer_ctry"    

        Input:  select count(Id), sum(sales),Country from tblCustomer_ctry
                  group by country
      Output:

SUBQUERY: A subquery is a query within another SQL query and embedded within WHERE clause.
  •  It is also called Nested Query.
  • In this case firstly executes its inside queries and after that outside queries.
  • Above two table has created tblCustomer and tblAddress .
            Input: select * from tblAddress
                      where CustomerId_fk in
                      (select Id from tblCustomer)

     Co-related queries : In this Queries ,first executes outside queries and send        the data for inside execution and again send the data from inside to outside. 
            Input: select * from tblAddress
                      where CustomerId_fk in
                      (select Id from tblCustomer where Id=CustomerId_fk)

                                             Thank You...


No comments:

Post a Comment