- Back to Home »
- sql server »
- SQL Server 2012 Select query
Posted by :
Sudhir Chekuri
Friday, 22 November 2013
Select
select 'test'When you execute the above select query it will print test as output
select * from tbl_register
The above query is used to print the whole table data named as tbl_register.
select name from tbl_register
The above query is used to print data of name column from table named as tbl_register.
Where
select name from tbl_register where id<3The above query is used to print data of name column from tbl_register table with id value less than 3 only.
select id, name from tbl_register where id<3 order by name
The above query is used to print data from id and name columns and the output will be sorted based on the name column data.
Distinct
select distinct name from tbl_registerThe above query will print data in name column without duplicates or repeated values.
alias
select name as username from tbl_register
select username = name from tbl_register
select name username from tbl_register
The above three queries will print data in name column with username as column name.
select name from tbl_register as register
select name from tbl_register register
The above two queries will create register as alias name for table name tbl_register which is used in joins.
case
select id, name,
case stat
when 'yes' then 'pass'
else 'fail'
end as
[status]
from tbl_register
The above query will print three columns id, name, status.
id, name columns data is printed along with one more column named as status which is not present in table.
the data under status column is based on stat column which is present under tbl_register.
if stat column value is yes then at that place pass is printed else fail is printed in status column in the output.
id, name columns data is printed along with one more column named as status which is not present in table.
the data under status column is based on stat column which is present under tbl_register.
if stat column value is yes then at that place pass is printed else fail is printed in status column in the output.
id name status
1 sudhir pass
2 raj fail
3 ram pass
4 gopi pass
5 sudhir fail
Joins
Inner joins
Outer joins
Cross joins