Advertisement
IMustRemainUnknown

SQL CODES

Aug 12th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. SELECT * FROM table_name;
  2.  
  3. SELECT DISTINCT Country FROM Customers;
  4.  
  5. SELECT * FROM Customers WHERE Country='Mexico';
  6.  
  7.  
  8. ===== AND, OR, NOT =====
  9.  
  10. SELECT * FROM Customers
  11. WHERE Country='Germany' AND City='Berlin';
  12.  
  13. SELECT * FROM Customers
  14. WHERE City='Berlin' OR City='München';
  15.  
  16. SELECT * FROM Customers
  17. WHERE NOT Country='Germany';
  18.  
  19.  
  20.  
  21. ===== ORDER BY =====
  22.  
  23. SELECT * FROM Customers
  24. ORDER BY Country;
  25.  
  26. SELECT * FROM Customers
  27. ORDER BY Country DESC;
  28.  
  29. SELECT * FROM Customers
  30. ORDER BY Country ASC, CustomerName DESC;
  31.  
  32.  
  33.  
  34. ===== INSERT INTO =====
  35.  
  36. INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
  37. VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
  38.  
  39.  
  40. ===== NULL =====
  41.  
  42. SELECT CustomerName, ContactName, Address
  43. FROM Customers
  44. WHERE Address IS NULL;
  45.  
  46. SELECT CustomerName, ContactName, Address
  47. FROM Customers
  48. WHERE Address IS NOT NULL;
  49.  
  50.  
  51.  
  52. ===== UPDATE =====
  53.  
  54. UPDATE Customers
  55. SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
  56. WHERE CustomerID = 1;
  57.  
  58. Be careful when updating records. If you omit the WHERE clause, ALL records will be updated!
  59.  
  60.  
  61.  
  62. ===== DELETE =====
  63.  
  64. DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
  65.  
  66. Be careful when deleting records in a table! Notice the WHERE clause in the DELETE statement. The WHERE clause specifies which record(s) should be deleted. If you omit the WHERE clause, all records in the table will be deleted!
  67.  
  68.  
  69. ===== SELECT TOP =====
  70.  
  71. SELECT TOP 3 * FROM Customers;
  72. SELECT TOP 50 PERCENT * FROM Customers;
  73. SELECT MIN(Price) AS SmallestPrice
  74. FROM Products;
  75.  
  76.  
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement