SQL 101 or Transact SQL Basic
The following assume that you have downloaded the Microsoft SQL Server Manager Studio Express. This is free software so use the custom Google search box on the right (under Search) and search for Microsoft SQL Server Manager Studio Express. You will be taken to www.microsoft.com/downloads....
What is SQL?
Structured Query Language is a database computer language designed for managing data in relational database management systems (RDBMS). Think of it as a Language that you write to get information stored somewhere. The database you get after you install Microsoft SQL Server Manages Studio Express is Northwind. It will be use though out this tutorial. Refer to SQL Server Tips for more info on this database.
Structured Query Language is a database computer language designed for managing data in relational database management systems (RDBMS). Think of it as a Language that you write to get information stored somewhere. The database you get after you install Microsoft SQL Server Manages Studio Express is Northwind. It will be use though out this tutorial. Refer to SQL Server Tips for more info on this database.
How do you select data?
select * from dbo.[Customers]
It will give you all the data that is store in the [Customers] table. If you have 1000 rows in the table it will try to load all the data for you. You can enter top 10 if you only want it to return the first 10 rows if you do not know what kind of info information is stored in this table.
select * from dbo.[Customers]
It will give you all the data that is store in the [Customers] table. If you have 1000 rows in the table it will try to load all the data for you. You can enter top 10 if you only want it to return the first 10 rows if you do not know what kind of info information is stored in this table.
WHERE clause
It set the condition or limit the data you get
= the field needs to match eg. [Country] = 'USA'
in the field match more than one eg. [Country] in ('USA', 'UK')
like the field start with, end with or contain any part of a string. Eg. [Country] like 'A%' return Argentina, Austria, Argentina, Austria, and Argentina. Eg. [Country] like '%A' will give you all the countries that end with A. Eg. [Country] like '%A%' give you all the coutries that will have an A in the field no matter where it appears.
= Query: select * from dbo.[Customers]where [Country] = 'USA'
in Query: select * from dbo.[Customers]where [Country] in ('USA', 'UK')
like Query: select * from dbo.[Customers]where [Country] like 'A%'
Let said you want to know how many Customers you have in your database.
select count(*) as total_rec from dbo.[Customers]
If you want to know how many customers you have by country.
select [Country], count(*) as total_rec from dbo.[Customers] group by [Country]
order by total_rec desc
It set the condition or limit the data you get
Let said you want to know how many Customers you have in your database.
select count(*) as total_rec from dbo.[Customers]
If you want to know how many customers you have by country.
select [Country], count(*) as total_rec from dbo.[Customers] group by [Country]
order by total_rec desc
Working with Dates
Date in US format: convert(varchar, OrderDate, 101)
Date in UK format: convert(varchar, OrderDate, 103)
Get Today Date: convert(varchar,getdate(),101)
Get Max or Min date max([OrderDate]
The folloiwng query will get you an example of what they will look like.
select CustomerID, OrderDate,
convert(varchar, OrderDate, 101) as US_Date,
convert(varchar, OrderDate, 103) as UK_Date,
convert(varchar,getdate(),101) as Today_Date
from dbo.Orders where [OrderDate]= (select max([OrderDate]) from dbo.Orders)
Other Date Example
select CustomerID, OrderDate,
month(OrderDate) as theMonth, day(OrderDate) theDay, year(OrderDate) theYear,
datename(mm, OrderDate) as MonthName,
datediff(dd,OrderDate , GetDate()) as Day_from_today,
dateadd(dd, 30, OrderDate) as Date_add_30,
DATEPART(WW, OrderDate) AS 'WEEK',
DATEPART(QQ, OrderDate) AS 'QUARTER'
from dbo.Orders where [OrderDate]= (select max([OrderDate]) from dbo.Orders)
convert(varchar, OrderDate, 101) as US_Date,
convert(varchar, OrderDate, 103) as UK_Date,
convert(varchar,getdate(),101) as Today_Date
from dbo.Orders where [OrderDate]= (select max([OrderDate]) from dbo.Orders)
month(OrderDate) as theMonth, day(OrderDate) theDay, year(OrderDate) theYear,
datename(mm, OrderDate) as MonthName,
datediff(dd,OrderDate , GetDate()) as Day_from_today,
dateadd(dd, 30, OrderDate) as Date_add_30,
DATEPART(WW, OrderDate) AS 'WEEK',
DATEPART(QQ, OrderDate) AS 'QUARTER'
from dbo.Orders where [OrderDate]= (select max([OrderDate]) from dbo.Orders)
Working with Case
Case statement help you display or format the way your data look. Example the Status code can be 0 or 1, but your users do not know what they mean if you display it on a report. You can use case so that it display Open for 1 and Closed for 0.
More Example of Case Query:
select ProductName, UnitPrice, UnitsInStock,
case when UnitsInStock < 1 then 'ReOrder Now' when UnitsInStock between 1 and 9 then 'Running Low' when UnitsInStock between 10 and 50 then 'OK' when UnitsInStock > 50 then 'Have too much' else 'N/A' end as ReOrder_Status,
case when UnitsInStock < 1 then 'Not Available' else 'Available' end as InStock from dbo.Products
case when UnitsInStock < 1 then 'ReOrder Now' when UnitsInStock between 1 and 9 then 'Running Low' when UnitsInStock between 10 and 50 then 'OK' when UnitsInStock > 50 then 'Have too much' else 'N/A' end as ReOrder_Status,
case when UnitsInStock < 1 then 'Not Available' else 'Available' end as InStock from dbo.Products
Working with String
SELECT RTRIM(' Sample '), LTRIM(' Sample ')
SELECT RegionDescription, REPLACE(RegionDescription,'ern','') as NewDescription FROM dbo.Region