I hope you guys have gone through my previous tutorial SQL – A database language. Now let’s move little bit further to write basic SQL queries.
Since you guys are beginners in SQL, i am going to start with a basic SQL query syntax. Later we will go through lots of complex syntax.
— Basic SQL Query Syntax
SELECT <Column Names>
FROM <Table Name>
WHERE <Filter Condition>
GROUP BY <Column Name>
HAVING <Filter Condition>
ORDER BY <Column Name> ASC/DESC
Line 1: This is Single line comment in SQL. This will not be executed. To comment multiple lines at a time(block comment), include the text to be commented in between /* and */
Line 2: Select list consists of the required column names data to be displayed. To display all columns data, use SELECT *
Line 3: List of table names from which data to be fetched.
Line 4: Condition to filter the data.
Line 5: To group the data based on columns.
Line 6: Condition to filter the data when aggregate functions are used in select list.
Line 7: To sort the data in ascending or descending order. By default it is ascending.
— Example: To fetch EmployeeID and EmployeeName values of dbo.Employee table.
SELECT EmployeeName,Designation, Sum(Sal)
WHERE EmployeeID IN (1,2,3)
GROUP BY EmployeeName,Designation
HAVING COUNT(EmployeeName) = 1
ORDER BY EmployeeID DESC
Well. We are able to write basic query in SQL Before writing queries further, remember writing queries that provide you with the required result is not the only thing required but following the standards also equally important I am going to discuss database standards to be followed while writing SQL queries in my coming tutorials.