This is video tutorial on
How to write simple SQL queries? or Learn simple SQL queries
SQL queries are versatile, and they can be utilized for numerous operations, including creating and modifying tables, inserting, updating, or deleting data from tables, selecting data from one or multiple tables based on specified conditions, joining tables to combine data from various tables, and more.
Before writing complex SQL queries you must need to understand basic syntax and process of writing SQL queries.
This video tutorial is about Learn basic SELECT query writing with different scenario and condition. You will also learn various operator is getting used in SELECT queries for performing data filtering based on certain condition.
Below sample queries can help you to start writing SELECT queries.
--Simple select to get data from table
select * from dbo.Comments
--If you want to get only top 100 records
SELECT top 100 * from dbo.Comments
--If you want only few column in query output
select top 1000 ID,text,creationDate from dbo.Comments
--Where Clause used to filter data based on condition.
SELECT * from dbo.Comments
WHERE ID = 4
--Where Condition with different column name
SELECT * from dbo.Comments
WHERE Score = 8
-- if want data for score 8 and ID 51592. AND clause will used to filter data.
SELECT * from dbo.Comments
WHERE ID = 51592 and SCORE = 8
--IF want data for SCORE 8 and ID 51592, 16502,17832,17178
SELECT * from dbo.Comments
WHERE ID IN(51592, 16502,17832,17178)
AND Score = 8
--IF want data for score 8 or 2
SELECT * from dbo.Comments
WHERE Score =8 or Score =4
--Now we will order data based on column
--Now will order data based on ID desc
SELECT top 5 * from dbo.Comments
ORDER BY ID DESC
--will compare both
SELECT top 5 * from dbo.Comments
ORDER BY ID
--Group by Clause help to group data based on Condition. To use group by we need to use aggregate function like SUM, count etc.
--Query to group data based on CreationDate
select CreationDate,count(*) from dbo.Comments
group by CreationDate
--Group data based on Year
select YEAR(CreationDate),count(*) from dbo.Comments
group by YEAR(CreationDate)
--Group data and filter it using having clause
select YEAR(CreationDate),count(*) from dbo.Comments
group by YEAR(CreationDate)
having YEAR(CreationDate) > '2009'