Mastering SQL Commands for Efficient Database Querying

sql command 01 n.w
1 / 60
Embed
Share

Explore the fundamentals of SQL commands such as SELECT, DISTINCT, WHERE, =, >, =,

  • SQL Commands
  • Database Querying
  • Data Retrieval
  • Query Optimization
  • Structured Query Language

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. SQL Command 01

  2. Select Table Field name SELECT column1, column2, ... FROM table_name; Field SELECT * FROM table_name SELECT Product_id, Product_nameEng from T_Product SELECT * FROM T_Product

  3. Select >> From >> Where >> Order by Select >> From >> Where >> group by Select >> From >> Where >> group by>> Order by

  4. SELECT DISTINCT Distinct() SELECT DISTINCT column1, column2, ... FROM table_name; SELECT DISTINCT(Receipt_no) from T_Sales_Detail

  5. SELECT DISTINCT select Receipt_no from T_Sales_Detail SELECT DISTINCT(Receipt_no) from T_Sales_Detail Receipt_No Receipt_No F16012019G300001 F16012019G300001 F16012019G300001 F16012019G300001 F16012019G300001 F16012019G300002 F16012019G300002 F16012019G300002 F17012019G300001 F17012019G300001 F17012019G300001 F17012019G300002 F17012019G300002 F17012019G300002 F16012019G300001 F16012019G300002 F17012019G300001 F17012019G300002

  6. Where SELECT column1, column2, ... FROM table_name WHERE condition SELECT column1, column2, ... FROM table_name WHERE condition1 and condition2 and condiion3 SELECT column1, column2, ... FROM table_name WHERE condition1 or condition2 or condition 3

  7. = : > : < : >= : <= : <> :

  8. = > < >= <= <> Equal to Greater than Less than Greater than or equal to Less than or equal to Not equal to BETWEEN LIKE IN

  9. Where select Product_Id,Product_NameEng, Unit_Price,Product_Type_Id from T_Product where Unit_Price >=3000 select Product_Id,Product_NameEng, Unit_Price,Product_Type_Id from T_Product where Unit_Price =3000

  10. And, Or, Not SELECT column1, column2, ... FROM table_name WHERE condition1 and condition2 and condiion3 SELECT column1, column2, ... FROM table_name WHERE condition1 or condition2 or condition 3 SELECT column1, column2, ... FROM table_name WHERE NOT condition; Or = And = Not =

  11. And, Or, Not selectProduct_Id,Product_NameThai, Unit_Price,Product_Type_Id fromT_Product whereUnit_Price>50 orProduct_Type_Id='PT006' select Product_Id,Product_NameThai, Unit_Price,Product_Type_Id from T_Product where Unit_Price >50 and Product_Type_Id='PT006' Product_Id G001 G002 G003 G004 G006 G007 G008 G009 G011 G012 G013 G014 G015 G016 G017 G018 G019 Product_NameThai Unit_Price Product_Type_Id PT001 PT009 PT006 PT006 PT008 PT006 PT009 PT006 PT002 PT006 PT004 PT008 PT006 PT008 PT009 PT008 PT008 60 200 20 10 Product_Id Product_NameThai G012 G015 Unit_Price Product_Type_Id PT006 PT006 190 60 20000 40 150 20 200 190 290 3000 60 2000 60 30000 20000

  12. And, Or, Not select Product_Id,Product_nameEng from T_Product where not Product_nameEng ='Beer' select Product_Id,Product_nameEng , Unit_Price from T_Product where Unit_Price >10 and (Product_nameEng ='Beer' or Product_nameEng='Coke')

  13. Order by Desc|Asc Order by Desc Asc SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;

  14. Order by Desc|Asc select Product_Id,Product_nameEng,Unit_price from T_Product order by unit_price desc select Product_Id,Product_nameEng,Unit_price from T_Product order by unit_price asc

  15. Select WhereOrder by Select field name /[*][Count(?)] From table name [Where condition] [Order by field name desc/asc] Desc Asc Select Product_name From Product Where Product_id= G001 Select * From TPS_Product Where Product_Id <>'G001 Order by field name desc

  16. Null, Not Null SQL Command Description Null SELECT column_names FROM table_name WHERE column_name IS NULL Null SELECT column_names FROM table_name WHERE column_name IS NOT NULL;

  17. Null, Not Null SQL Command Description Null select * from Brand where brand_name is null Null select * from Brand where brand_name is not null

  18. Select Top SELECT TOP number|percent column_name(s) FROM table_name WHERE condition; SELECT column_name(s) FROM table_name WHERE condition LIMIT number; **** Run

  19. Select Top SELECT TOP 3 * FROM T_Product SELECT TOP 50 PERCENT * FROM T_Product

  20. Select Top SELECT TOP number|percent column_name(s) FROM table WHERE condition Order by condition asc|desc Ex1 : Select top 10 Unit_Price Ex2 : Select top 10 Unit_Price from Product order by Unit_Price desc

  21. Select Function Select Min ( ), Select Max ( ), Select AVG ( ), Select Count ( ), Select Sum ( )

  22. Select MIN, MAX SELECT MIN(column_name) FROM table_name WHERE condition; SELECT MAX(column_name) FROM table_name WHERE condition;

  23. Select MIN, MAX select MIN(Unit_price) from T_Product select MAX(Unit_price) from T_Product select MAX(Unit_price) from T_Product where product_type_id='PT001'

  24. Select Count, AVG, SUM SELECT COUNT(column_name) FROM table_name WHERE condition SELECT AVG(column_name) FROM table_name WHERE condition SELECT SUM(column_name) FROM table_name WHERE condition

  25. Select Count, AVG, SUM Product_Id Product_NameEng Unit_Price Product_Type_Id G001 Beer G005 Coke G010 Green Tea G020 Water G011 Make up G013 Medicine G012 Meat G015 milk G007 Cookie G003 Bread G004 Candy G009 Egg G006 Computer G016 Printer G018 Telephone G019 Television G014 Microwave G017 Shampoo G008 Diaper G002 Blanket 60 12 20 12 PT001 PT001 PT001 PT001 PT002 PT004 PT006 PT006 PT006 PT006 PT006 PT006 PT008 PT008 PT008 PT008 PT008 PT009 PT009 PT009 select COUNT(Product_Id) from T_Product where product_type_id='PT001' 200 290 190 60 40 20 10 20 select AVG(unit_price) from T_Product where product_type_id='PT001' 20000 2000 30000 20000 3000 select sum(unit_price) from T_Product where product_type_id='PT001 60 150 200

  26. LIKE SELECT column1, column2, ... FROM table_name WHERE columnN LIKE pattern;

  27. LIKE LIKE Operator Description WHERE CustomerName LIKE 'a%' Finds any values that start with "a" WHERE CustomerName LIKE '%a' Finds any values that end with "a" WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"

  28. LIKE select Product_Id,Product_nameEng, Unit_price,Product_Type_Id from T_Product where Product_NameEng like'B% >>>> ( B) select Product_Id,Product_nameEng, Unit_price,Product_Type_Id from T_Product where Product_NameEng like'%B%' >>>> ( B ) select Product_Id,Product_nameEng, Unit_price,Product_Type_Id from T_Product where Product_NameEng like'%B' >>>> ( B)

  29. LIKE select emp_no,emp_name,job_no from Employee where Emp_Name like'% select emp_no,emp_name,job_no from Employee where Emp_Name like' % select emp_no,emp_name,job_no from Employee where (Emp_Name like'% %')

  30. Select Where value in SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, ...); SELECT column-name FROM table-name1 WHERE column-name IN (SELECT STATEMENT) https://www.dofactory.com/s ql/subquery

  31. Select Where value in select Product_Id,Product_nameEng, Unit_price,Product_Type_Id from T_Product where Product_NameEng in ('Beer','Blanket','Bread') select Product_Id,Product_nameEng, Unit_price,Product_Type_Id from T_Product where Product_NameEng not in ('Beer','Blanket','Bread')

  32. Select Where value in SELECT column-names SELECT * FROM table-name1 WHERE value IN (SELECT column-name FROM table-name2 WHERE condition) select * from T_Sales_detail where Product_Id in (Select Product_Id from T_Product where Product_Type_Id ='PT001') order by Receipt_No asc

  33. Select Where value in select * from T_Sales_detail where Product_Id in (Select Product_Id from T_Product where Product_Type_Id ='PT001') order by Receipt_No asc select A.Receipt_no,A.Product_Id,A.Product_nameEng, A.Qty,A.Unit_Price,A.Total_amt,B.Product_Type_Id from T_Sales_detail A, T_Product B where B.Product_Type_Id ='PT001' and A.Product_Id=B.Product_Id order by Receipt_No asc

  34. Between SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2

  35. Between select Product_Id,Product_nameEng, Unit_price,Product_Type_Id from T_Product where Unit_price between 20 and 50 Between And A,B select Product_Id,Product_nameEng, Unit_price,Product_Type_Id from T_Product where Unit_price >= 20 and Unit_price<=50

  36. Not Between select Product_Id,Product_nameEng, Unit_price,Product_Type_Id from T_Product where Unit_price not between 20 and 50

  37. GROUP BY SELECT column_name(s), [Count()],[ Sum()] FROM table_name WHERE condition GROUP BY column_name(s) ORDER BY column_name(s);

  38. GROUP BY select Product_type_id,count(Product_id)as Count from T_Product group by Product_type_id Product_Type_Id PT001 PT002 PT004 PT006 PT008 PT009 Count 4 1 1 6 5 3 select Receipt_No,SUM(Total_Amt) as Sum from T_sales_detail group by Receipt_No Receipt_No F16012019G300001 F16012019G300002 F17012019G300001 F17012019G300002 Sum 222 280 92 170

  39. HAVING HAVING WHERE SQL WHERE HAVING GROUP BY( Group Having) Select column_name(s)/Count/Sum From Table Where Condition1 Group by column_name(s) Having Condition2 Order by Column/Count/Sum

  40. having Select > From > Where > Group by > Having > Order by Select Column/Count/Sum From Table Where Condition Group by Column Having Condition2 Order by Column/Count/Sum

  41. HAVING The HAVING clause allows you to specify conditions on the results of aggregate functions (such as SUM, AVG, COUNT, etc.) HAVING clause an aggregate function WHERE clause an aggregate function. GROUP BY HAVING You can use the WHERE clause without the GROUP BY clause, But you cannot use the HAVING clause without the GROUP BY clause having GROUP BY https://www.sqlservertutorial.net/sql-server-basics/sql-server-having/

  42. HAVING having Sum, Count,avg SELECT Product_Id, SUM(Total_amt) as Tot FROM sales_detail WHERE Product_Id>'G001' GROUP BY Product_Id HAVING SUM(Total_amt)>2000 ORDER BY Tot asc

  43. Having Sum ***() SELECT Product_Id, SUM(Total_amt) as Tot FROM sales_detail WHERE Product_Id>'G001' GROUP BY Product_Id HAVING SUM(Total_amt)>1300 ORDER BY Tot asc SELECT Product_Id, SUM(Total_amt) as Tot FROM sales_detail WHERE Product_Id>'G001' GROUP BY Product_Id HAVING Product_Id='G012' ORDER BY Tot asc SELECT Product_Id, SUM(Total_amt) as Tot FROM sales_detail WHERE Product_Id>'G001' GROUP BY Product_Id HAVING SUM(Total_amt)>1300 and Product_Id='G012' ORDER BY Tot asc

  44. Having > Correct VS Wrong Select From Sales,Sales_Detail Where sales.Receipt_No=Sales_Detail.Receipt_No Group by Sales.Cust_Id,Sales_Detail.Product_Id Having SUM(Total_amt) <=2000 Order by Sales.Cust_Id,Product_Id Sales.Cust_Id,Sales_Detail.Product_Id, SUM(Total_amt) as TotTal Select From Sales,Sales_Detail Where sales.Receipt_No=Sales_Detail.Receipt_No and SUM(Total_amt) <=2000 Group by Sales.Cust_Id,Sales_Detail.Product_Id Order by Sales.Cust_Id,Product_Id Sales.Cust_Id,Sales_Detail.Product_Id, SUM(Total_amt) as TotTal

  45. Having > Compare many SQL commands Select From Sales,Sales_Detail Where sales.Receipt_No=Sales_Detail.Receipt_No Group by Sales.Cust_Id,Sales_Detail.Product_Id Having SUM(Total_amt) >1300 Order by Sales.Cust_Id,Product_Id Sales.Cust_Id,Sales_Detail.Product_Id, SUM(Total_amt) as TotTal Select From Sales,Sales_Detail Where sales.Receipt_No=Sales_Detail.Receipt_No and Sales.Cust_Id='C002' Group by Sales.Cust_Id,Sales_Detail.Product_Id Having SUM(Total_amt) >1300 Order by Sales.Cust_Id,Product_Id Sales.Cust_Id,Sales_Detail.Product_Id, SUM(Total_amt) as TotTal Select From Sales,Sales_Detail Where sales.Receipt_No=Sales_Detail.Receipt_No Group by Sales.Cust_Id,Sales_Detail.Product_Id Having SUM(Total_amt) >1300 and Sales.Cust_Id='C002' Order by Sales.Cust_Id,Product_Id Sales.Cust_Id,Sales_Detail.Product_Id, SUM(Total_amt) as TotTal

  46. Having ( Count,Avg,) select * from sales_detail Order by receipt_no Select receipt_no,Count(product_id) as count From Sales_detail Group by receipt_no Having count(product_id) >1 Order by receipt_no Select receipt_no,avg(Total_amt) as count From Sales_detail Group by receipt_no Having avg(Total_amt) >=200 Order by receipt_no

  47. having Select Sales.Cust_Id,Sales_Detail.Product_Id From Sales,Sales_Detail Where sales.Receipt_No=Sales_Detail.Receipt_No Group by Sales.Cust_Id,Sales_Detail.Product_Id Having Product_Id ='G005' Order by Product_Id

  48. Alias (): Alias Column Syntax SELECT column_name AS alias_name FROM table_name; select Product_Id as Id, Product_nameEng as Name from T_Product

  49. Alias (): Alias Table Syntax SELECT column_name(s) FROM table_name AS alias_name; select A.Product_Id ,A.Product_nameEng from T_Product as A

  50. INSERT 1 Row SQL Command Description Field Field Table INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...) Field name Field Field Table INSERT INTO table_name VALUES (value1, value2, value3, ...)

More Related Content