SELECT veri çekme komutudur. SELECT sözcüğü ile veritabanımızdaki herhangi bir tabloya bağlanarak istediğimiz bilgileri seçebiliriz. " * " işareti ile tablodaki bütün alanları seçebiliriz. FROM tablolardan veri çekmemizi sağlar. WHERE sorgusu ile istediğimiz koşullar belirleyebiliriz.
Uygulama tablolarımız sql server deki Northwind database deki tablolardır
Customers(CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax)
Employees(EmployeeID, LastName, FirstName, Title, TitleOfCourtesy, BirthDate, HireDate, Address, City, Region, PostalCode, Country, HomePhone, Extension, Photo, Notes, ReportsTo, PhotoPath)
NOT : New Query ile yeni bir sayfa açtıktan sonra USE NORTHWİND ile sorgunuza başlamalısınız. Burada hangi database ile çalıştığınızı belirtiyorsunuz
use Northwind
-- customers tablosundaki tüm verileri listelemek
select * from [Customers]
-- customers tablosundaki ilk 5 veriyi listelemek
select top(5)* from [Customers]
-- customers tablosundaki CompanyName leri listelemek
select CompanyName from [Customers]
-- customers tablosundaki ilk 10 CompanyName leri listelemek
select top(3) CompanyName from [Customers]
-- customers tablosundaki city=berlin olanlarý listele
select * from [Customers] where City='Berlin'
-- customers tablosundaki Postalcode si 10000 ile 20000 arasýnda olanlarý listele
select * from [Customers] where PostalCode > '10000' and
PostalCode < '20000'
-- customers tablosundaki Postalcode si 10000 ile 20000 arasýnda ve eþit olanlarý listele
select * from [Customers] where PostalCode >= '10000' and
PostalCode <= '20000'
-- customers tablosundaki CompanyName' in ilk harflerinin g den önceki ile baþlayanlarýnýn listelenmesi
select * from [Customers] where CompanyName < 'g'
--Employees tablosundaki employeeid=5 olaný listele
select * from [Employees] where EmployeeID=5
--Employees tablosundaki employeeid <>5 farklý olanlarý listele
select * from [Employees] where EmployeeID <> 5
--Employees tablosundaki 1948-12-08 tarihli kayýtý listele
select BirthDate from [Employees] where BirthDate='1948-12-08'
--Employees tablosundaki 1948-12-08 ile 1958-10-10 tarihi arasýndaki kayýtlarý listele
select BirthDate from [Employees] where BirthDate>'1948-12-08'
and BirthDate<'1958-10-10'
--Employees tablosundaki EmployeeID si 5 ile 10 arasýnda olanlarý yeni column da listelemek
select EmployeeID as yeni from [Employees] where EmployeeID>5
and EmployeeID<10
--Employees tablosundaki City den kaç adet olduðunu listele
select City, count(*) 'kactane' from [Customers]
GROUP BY City
HAVING City IS NOT NULL
ORDER BY City