Getting Started With Basic SQL Keywords And SQL Join

This article starts with the basics of the SQL keyword. These keywords are for data filtration in a SQL database table.

From

Command:

  1. Select * From TableName  
  2. Select Column From TableName  
Example:
  1. select * from Info   
  2. select Name from Info  

Where

Command:

  1. Select * from TableName where Condition  
  2. Select column1, column2 from TableName where Condition  
Example:
  1. select * from Info where City = 'Ahmedabad'  
  2. select Name from Info where city = 'Surat'  


Top

This command selects the top rows in a database table.

Command:

  1. Select Top value from TableName  
Example:
  1. select Top 3 * from Info   
  2. select Top (25) percent * from Info  


As

Use for Alias Keyword.

Command:

  1. Select ExitingColumnName AS NewAliasColumnName from TableName  
Example:
  1. Select Name From Info  
  2. select Name AS [Person Namefrom Info  


AND

Command:

  1. select * from TableName where condition1 AND Condition2  
  2. select ColumnName from TableName where condition1 AND Condition2  
Example:
  1. select * from Info where Name='Rakesh' And City= 'Ahmedabad'  
  2. Select * From info where Name='Rakesh' And City = 'Surat'  


OR

Command:

  1. select * from TableName where condition1 OR Condition2  
  2. select ColumnName from TableName where condition1 OR Condition2  
Example:
  1. select * from Info where Name='Rakesh' OR City= 'Ahmedabad'  
  2. Select * From info where Name='Rakesh' OR City = 'Surat'  


Between

Command:

  1. Select * from TableName where columnCondition Between StartValue AND EndValue  
Example:
  1. select * from Info  
  2. select * from info where id Between 3 AND 5  


IN

Command:

  1. select * from TableName where condition IN (‘DATAValue’)  
Example:
  1. select * from Info where city in ('Ahmedabad')  
  2. select * from Info where city in ('Goa','Surat')  


LIKE

Command:

  1. select * from TableName From ColumnName LIKE ‘%VALUE%’  
Example:
  1. select * from Info where city like '%Goa%'  
  2. Select * from Info Where City like '%Ahm%'  
  3. Select * from Info where Name like '%esh%'  
  4. Select * from Info where id like '%3%'  


Distinct

Don't display the same multiple records.

Command:

  1. Select DISTINCT ColumnName From TableName  
Example:
  1. select City from Info  
  2. Select DISTINCT City from Info  


Order By

Sorts data in ascending or descending order.

Command:

  1. Select * from TableName Order By ColumnName ASC | DESC  
  2. Select columnName from TableName Order By ColumnName ASC | DESC  
Example:
  1. Select * from Info order by Name ASC --ascending order by Name Column  
  2. Select Name from Info order by Name DESC --descending order by Name Column  


Group By

Selects data in a group.

Command:

  1. Select conditionwithcolumn from TableName Group by ColumnName  
Example:
  1. select city, count(NameAs Number_of_Person from Info Group by City  


Counts the total number of pepole in each city.

SQL Join

A join is the most important concept of SQL databases. Many types of joins are available in SQL Server.

In this, any two or more tables are joined with the same data column field.

Basic Command
  1. Select column1, column2   
  2. From Table1  
  3. [Type of Join] Table2   
  4. ON condition  
  1. Cross Join

    All data records are selected from the selected database table.

    Command:
    1. Select column from Table1 CROSS JOIN Table2   
    Example:
    1. select EmployeeData.EmployeeID,EmployeeData.EmployeeName,Department.Department  
    2. from EmployeeData  
    3. Cross Join Department  


    If 5 data records are in Table1 and 2 data records are in Table2 then that means in a cross join the output is from both tables and are multiple records.
    Example: [5 record * 2 record = Total 10 records display]

  2. Inner Join

    Command:
    1. Select Table1.column,  
    2. Table2.column,  
    3. From Table1   
    4. Inner join Table2 ON Table1.ColumnID = Table2.ColumnID  
    Example:
    1. select EmployeeData.EmployeeID,  
    2. EmployeeData.EmployeeName,  
    3. Department.Department  
    4. From EmployeeData  
    5. Inner Join Department  
    6. ON EmployeeData.DepartmentID=Department.DepartmentID  


  3. Outer Join

    Two types of outer joins in SQL database.

    Left Outer Join

    Command:
    1. Select Table1.column,  
    2. Table2.column,  
    3. From Table1   
    4. Left outer join Table2 ON Table1.ColumnID = Table2.ColumnID  
    Example:
    1. select EmployeeData.EmployeeID,EmployeeData.EmployeeName,  
    2. Department.DepartmentID,Department.Department  
    3. From EmployeeData  
    4. LEFT Outer Join Department  
    5. ON EmployeeData.DepartmentID=Department.DepartmentID  


    Right Outer Join

    Command:
    1. Select Table1.column,  
    2. Table2.column,  
    3. From Table1   
    4. Right outer join Table2 ON Table1.ColumnID = Table2.ColumnID  
    Example:
    1. select EmployeeData.EmployeeID,EmployeeData.EmployeeName,  
    2. Department.DepartmentID,Department.Department  
    3. From EmployeeData  
    4. Right Outer Join Department  
    5. ON EmployeeData.DepartmentID=Department.DepartmentID  

Union

Command:

  1. Select column1, column2   
  2. from Table1  
  3. TypeofJoin Teble2  
  4. ON Table1.ColumnID = Table2.ColumnID  
  5. UNION   
  6. Select column1, column2   
  7. from Table1  
  8. TypeofJoin Teble2  
  9. ON Table1.ColumnID = Table2.ColumnID  

Example:

  1. Select EmployeeID,EmployeeName,Department  
  2. From EmployeeData  
  3. Left outer Join Department   
  4. On EmployeeData.EmployeeID = Department.DepartmentID  
  5. Union  
  6. Select EmployeeID,EmployeeName,Department  
  7. From EmployeeData  
  8. Right outer Join Department   
  9. On EmployeeData.EmployeeID = Department.DepartmentID  



Union ALL

Command:

  1. Select column1, column2   
  2. from Table1  
  3. TypeofJoin Teble2  
  4. ON Table1.ColumnID = Table2.ColumnID  
  5. UNION ALL  
  6. Select column1, column2   
  7. from Table1  
  8. TypeofJoin Teble2  
  9. ON Table1.ColumnID = Table2.ColumnID  

Example:

  1. Select EmployeeID,EmployeeName,Department  
  2. From EmployeeData  
  3. Left outer Join Department   
  4. On EmployeeData.EmployeeID = Department.DepartmentID  
  5. Union ALL  
  6. Select EmployeeID,EmployeeName,Department  
  7. From EmployeeData  
  8. Right outer Join Department   
  9. On EmployeeData.EmployeeID = Department.DepartmentID



I hope you understand about SQL database condition keywords and joins.

Up Next
    Ebook Download
    View all
    Learn
    View all