Advertisement
akevintg

SQLearning(IN, EXISTS, Alias Subquery, etc)

Apr 17th, 2015
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 5.61 KB | None | 0 0
  1. /*SQL LEARNING (IN, EXISTS, Alias Subquery, ETC)
  2.  
  3. LINK SOAL : https://www.dropbox.com/s/v06snxk4ixi2sm7/O1-T0206-DF01-07-Edited.docx?dl=0
  4. LINK CREATE+INSERT : https://www.dropbox.com/s/iw9w7xb5sx6n6mt/Create%20%2B%20Insert.sql?dl=0
  5.  
  6. QUERY INI DIBUAT MENGUNAKAN SQL SERVER MANAGEMENT STUDIO(SSMS) 2014*/
  7.  
  8. /*1.    Display TreatmentId and TreatmentName where TreatmentId is ‘TM001’ or ‘TM002’.
  9. (in)*/
  10.  
  11. SELECT TreatmentId, TreatmentName
  12. FROM MsTreatment
  13. WHERE TreatmentId IN('TM001','TM002')--Di gunakan untuk menghemat waktu
  14.  
  15. /*2.    Display TreatmentName and Price where TreatmentTypeName is not ‘Hair Treatment’
  16.  or ‘Message / Spa’.(in, not in)*/
  17.  
  18. SELECT TreatmentName, Price
  19. FROM MsTreatment MT
  20. WHERE TreatmentTypeId IN(
  21.     SELECT TreatmentTypeId
  22.     FROM MsTreatmentType MTT
  23.     WHERE MTT.TreatmentTypeId=MT.TreatmentTypeId
  24.         AND TreatmentTypeName NOT IN('Hair Treatment','Message / Spa')
  25.     )
  26.  
  27. /*3.    Display CustomerName, CustomerPhone and CustomerAddress where the Customer had done the
  28. transaction on Friday and CustomerName is more than 8 characters.(in, datename, weekday,len)*/
  29.  
  30. SELECT CustomerName,CustomerPhone,CustomerAddress
  31. FROM MsCustomer MC
  32. WHERE CustomerId IN(--digunakan untuk penghubung tabel dan persyaratan
  33.     SELECT CustomerId
  34.     FROM HeaderSalonServices HSS
  35.     WHERE HSS.CustomerId=MC.CustomerId
  36.         AND DATENAME(weekday,TransactionDate)='Friday'
  37.     )
  38.     AND LEN(CustomerName)>8
  39.  
  40. /*4.    Display TreatmentTypeName, TreatmentName and Price where CustomerName contains the word
  41. ‘Putra’ and the transaction was happened on date 22.(in, like, day)*/
  42.  
  43. SELECT *
  44. FROM MsTreatment MT
  45. WHERE TreatmentId IN(
  46.     SELECT TreatmentId
  47.     FROM HeaderSalonServices HSS, MsCustomer MC, DetailSalonServices DSS
  48.     WHERE  HSS.CustomerId=MC.CustomerId AND HSS.TransactionId=DSS.TransactionId AND DSS.TreatmentId=MT.TreatmentId
  49.         AND CustomerName LIKE '%Putra%'
  50.         AND DAY(TransactionDate)=22
  51.     )
  52. /*5.    Display StaffName, CustomerName and TransactionDate (obtained from TransactionDate with format
  53. ‘Mon dd, yyyy’) where the last digit of TreatmentID is even.(convert, exists, right)*/
  54.  
  55. SELECT StaffName,CustomerName,TransactionDate=CONVERT(VARCHAR,TransactionDate,107)
  56. FROM MsStaff MS, MsCustomer MC, HeaderSalonServices HSS
  57. WHERE EXISTS (
  58.     SELECT *
  59.     FROM DetailSalonServices DSS
  60.     WHERE DSS.TransactionId=HSS.TransactionId AND RIGHT(TreatmentId,1)%2=0
  61.     )   --Hanya Untuk Mengecek, Return True/False,
  62.         --Biasa mengunakan EXISTS Untuk mengecek data yang tidak muncul (dalam kasus ini TreatmentID)
  63.     AND MC.CustomerId=HSS.CustomerId AND HSS.StaffId=MS.StaffId
  64.  
  65. /*6.    Display CustomerName, CustomerPhone and CustomerAddress where the Customer has been served
  66. by Staff whose length of the staff’s name is odd number.(exists, len)*/
  67.  
  68. SELECT CustomerName,CustomerPhone, CustomerAddress
  69. FROM MsCustomer MC
  70. WHERE EXISTS(
  71.     SELECT *
  72.     FROM MsStaff MS, HeaderSalonServices HSS
  73.     WHERE MS.StaffId=HSS.StaffId AND HSS.CustomerId=MC.CustomerId
  74.         AND LEN(StaffName)%2=1
  75.     )
  76.  
  77. /*7.    Display ID (obtained from the last 3 digit characters StaffId) and Name (obtained from the
  78. middle name of StaffName) where the Staff hasn’t served the Customer with ‘Male’ gender and StaffName
  79. contains 3 words or more.(right, substring, charindex, len, exists, in, not like, like)*/
  80.  
  81. SELECT ID=RIGHT(StaffID,3),
  82.     Name=SUBSTRING(SUBSTRING(StaffName,CHARINDEX(' ',StaffName)+1,LEN(StaffName)),
  83.         1,CHARINDEX(' ',SUBSTRING(StaffName,CHARINDEX(' ',StaffName)+1,LEN(StaffName))))
  84.         -- MIS Alexander Kevin W -> SUBSTRING(StaffName,CHARINDEX(' ',StaffName)+1,LEN(StaffName))
  85.         -- Dapet 'Kevin W' ->substring lagi SUBSTRING(Hasil Susbtring1,1(mulai dari index1),CHARINDEX Hasil Substring1)
  86.         -- Dapet Kevin, Yang hanya 2 nama, saat substring ke dua return NULL karena char index 0
  87. FROM MsStaff MS
  88. WHERE EXISTS(
  89.     SELECT *
  90.     FROM MsCustomer MC, HeaderSalonServices HSS
  91.     WHERE MC.CustomerId=HSS.CustomerId AND MS.StaffId=HSS.StaffId
  92.         AND CustomerGender NOT LIKE 'Male'
  93.     )
  94.     AND StaffId IN (
  95.         SELECT StaffId
  96.         FROM HeaderSalonServices HSS
  97.         WHERE HSS.StaffId=MS.StaffId
  98.     )
  99.     AND StaffName LIKE '% % %'
  100.  
  101. /*8.    Display TreatmentTypeName, TreatmentName and Price where Price is greater than the average
  102. of all Price.(alias subquery, avg)*/
  103.  
  104. SELECT TreatmentTypeName,TreatmentName,Price
  105. FROM MsTreatment MT, MsTreatmentType MTT,(--ALIAS SUBQUERY
  106.     SELECT [avgR]=AVG(price)--Row
  107.     FROM MsTreatment
  108.     )avgO--Object
  109. WHERE Price>avgO.avgR AND MT.TreatmentTypeId=MTT.TreatmentTypeId
  110.  
  111. /*9.    Display StaffName, StaffPosition and StaffSalary where StaffSalary is the highest or lowest salary.
  112. (alias subquery, max, min)*/
  113.  
  114. SELECT StaffName,StaffPosition,StaffSalary
  115. FROM MsStaff, (
  116.     SELECT [maxSalary]=MAX(StaffSalary), [minSalary]=MIN(StaffSalary)
  117.     FROM MsStaff
  118.     )DATA
  119. WHERE StaffSalary=DATA.maxSalary OR StaffSalary=DATA.minSalary
  120.  
  121. /*10.   Display CustomerName, CustomerPhone, CustomerAddress and Count Treatment (obtained from the amount of
  122. Treatment that is done per transaction) where Count Treatment is the highest Count Treatment.
  123. (count, alias subquery, max)*/
  124.  
  125. SELECT CustomerName, CustomerPhone,CustomerGender,[COUNT Treatment]=COUNT(TreatmentId)
  126. FROM MsCustomer MC,HeaderSalonServices HSS ,DetailSalonServices DSS,(
  127.             SELECT maks=MAX(jumlahTransaksi.totalTransactionPerTreatment)
  128.             FROM(
  129.                 SELECT totalTransactionPerTreatment=COUNT(TreatmentId)
  130.                 FROM DetailSalonServices
  131.                 GROUP BY TransactionID
  132.                 )jumlahTransaksi
  133.             )jumlahTransaksiPerOrang
  134. WHERE MC.CustomerId=HSS.CustomerId AND HSS.TransactionId=DSS.TransactionId
  135. GROUP BY HSS.TransactionId,CustomerName,CustomerPhone,CustomerGender,maks
  136. HAVING COUNT(TreatmentId)=jumlahTransaksiPerOrang.maks
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement