SQL Notes | 2 – NULL, UPDATE, DELETE, FUNCTIONS

NULL

-Customers tablosundaki postalcode değerleri boş olan tüm kayıtları çekmek için şu ifadeyi kullanabiliriz:

Select * from customers where postalcode is null;

-Customers tablosundaki postalcode değerleri boş olmayan tüm kayıtları çekmek için şu ifadeyi kullanabiliriz:

Select * from customers where postalcode is not null;

UPDATE

-Customer tablosunda City alanındaki tüm değerleri ‘Konya’ olarak güncellemek için şu ifadeyi kullanabiliriz:

Update customers set city = 'Konya';

-Customer tablosunda Country değeri ‘Türkiye’ olan tüm kayıtların City değerini ‘Konya’ olarak güncellemek için şu ifadeyi kullanabiliriz:

Update customers set city = 'Konya' where country = 'Türkiye';

-Customer tablosundaki customerid değeri 47 olan tüm kayıtların Country değerini ‘Türkiye’ ve City değerini de ‘Konya’ olarak güncellemek için şu ifadeyi kullanabiliriz:

Update customers set city = 'Konya', country = 'Türkiye' where customerid = 47;

DELETE

-Customers tablosundaki Country değeri ‘Türkiye’ olan kayıtları silmek için şu ifadeyi kullanabiliriz:

Delete from customers where country = 'Türkiye';

-Delete tablosundaki tüm kayıtları silmek için şu ifadeyi kullanabiliriz:

Delete from customers;

FUNCTIONS

-Products tablosundaki price değerlerin en küçüğünü bulmak için şu ifadeyi kullanabiliriz:

Select min(price) from products

-Products tablosundaki price değerlerin en büyüğünü bulmak için şu ifadeyi kullanabiliriz:

Select max(price) from products

-Products tablosundaki price değeri 18 olan kayıtların sayısını döndüren sorgu için şu ifadeleri kullanabiliriz:

Select count(*) from products where price = 18;
Select count(1) from products where price = 18; -- bu kod daha performanslı olur. tüm sütunları döndürmez

-Products tablosundaki tüm price değerinin ortalamasını bulmak için şu ifadeyi kullanabiliriz:

Select avg(price) from products;

-Products tablosundaki tüm price değerinin toplamını bulmak için şu ifadeyi kullanabiliriz:

Select sum(price) from products;

SQL Notes | 1 – SELECT , WHERE, ORDER BY, INSERT

SELECT 

-Customer tablosundaki tüm verileri çekmek için aşağıdaki ifadeyi kullanırız.

Select * from customers;

-Customers tablosundan City sütunundaki verileri çekmek için aşağıdaki ifadeyi kullanırız:

Select city from customers;

-Customer tablosundaki tüm farklı Country verilerini çekmek için aşağıdaki ifadeyi kullanabiliriz:

Select distinct countyr from customers;

WHERE

-City sütunundaki veriler ‘ İstanbul’ lanları çekmek için şu ifadeyi kullanabiliriz:

Select * from customers where city = ' İstanbul';

-NOT anahtar kelimesini kullanarak City verisi ‘ İstanbul’ olmayanları şöyle seçebiliriz:

Select * from customers where not city = ' İstanbul';

-Customers tablosundaki customerid değeri 18 olanları şu şekilde çekebiliriz:

Select * from customers where customerid = 18;

-Customers tablosundaki City değeri ‘İstanbul’ olan ve postalcode değeri 32200 olan kayıtları şu şekilde çekebilirz:

Select * from customers where city = ' İstanbul' and  postalcode = 32200;

-Customers tablosundaki City sütunundaki veriler ‘ İstanbul’ veya ‘Balıkesir’ olanlanları çekmek için şu ifadeyi kullanabiliriz:

Select * from customers where city = ' İstanbul' or city = 'Balıkesir' ;

ORDER BY

-Customers tablosundaki City sütunundaki verileri alfabetik olarak sıralı çekmek için şu ifadeyi kullanabiliriz:

Select * from customers where order by city;

-Customers tablosundaki Country ve City sütunundaki verileri, önce Country sonra da City için alfabetik olarak sıralı çekmek için şu ifadeyi kullanabiliriz:

Select * from customers where order by country, city;

INSERT

-Customer tablosuna yeni bir kayıt eklemeyi şu ifadeyle yapabiliriz:

Insert into Customers
(CustomerName,
Address,
City,
PostalCode,
Country)
values
('Ahmet Gürsoy',
'Beyoğlu',
'İstanbul',
'32000',
'Türkiye');