Advertisement
acarrunto

sql commands basic

Sep 16th, 2022 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. SELECT * FROM <table name>; // * (wild card) = all the column of the data.
  2. e.g. SELECT <column 1>, <column 2>, ... FROM <table name>;
  3.  
  4. SELECT <column 1> AS <new name of column 1> [, ...] FROM <table name>; // AS -> act as an alias for column names
  5. e.g. SELECT title AS Title, first_published AS "First Published" FROM <table name>;
  6.  
  7. SELECT <column 1>[, ...] FROM <table name> WHERE <condition>; // condition sintax is <column> <operator> <value>
  8. // date types must be written inside " "
  9. e.g. SELECT title, author, first_published FROM books WHERE author = "J.K. Rowling";
  10. SELECT book_id FROM loans WHERE loaned_on = "2015-12-10";
  11.  
  12. operator used: =, !=, >, >=, <, <=,
  13. e.g. SELECT * FROM books WHERE first_published > 2005;
  14.  
  15. logical used: AND, OR
  16. e.g. SELECT title FROM books WHERE author = "J.K. Rowling" AND first_published >= 2005;
  17.  
  18. SELECT <columns> FROM <table> WHERE <column> IN (<value 1>, <value 2>, ...);
  19. e.g. SELECT first_name, email FROM patrons WHERE library_id IN ("MCL1001", "MCL1100", "MCL1011")
  20. SELECT first_name, email FROM patrons WHERE library_id NOT IN ("MCL1001", "MCL1100", "MCL1011")
  21.  
  22. SELECT <columns> FROM <table> WHERE <column> BETWEEN <minimum> AND <maximum>;
  23. e.g. SELECT title, author FROM books WHERE first_published BETWEEN 1800 AND 1899;
  24. SELECT * FROM loans WHERE loaned_on BETWEEN "2015-12-13" AND "2015-12-19";
  25.  
  26. SELECT <columns> FROM <table> WHERE <column> LIKE <value>%; // search pattern are case insensitive
  27. .. %<value> ..
  28. .. %<value>% ..
  29. e.g. SELECT title FROM books WHERE title LIKE "harry potter%";
  30. SELECT title FROM books WHERE title LIKE "%universe%" AND genre = "Non Fiction"; // = must put the exact match, is case sensitive
  31. SELECT title FROM books WHERE title LIKE "%universe%" AND genre LIKE "non fiction";
  32.  
  33. = NULL // is wrong syntax
  34. IS NULL // is correct syntax
  35. SELECT <columns> FROM <table> WHERE <column> IS NULL;
  36. e.g. SELECT * FROM loans WHERE return_by > "2015-12-18" AND returned_on IS NULL; // NULL => represent a missing value
  37. SELECT * FROM loans WHERE return_by > "2015-12-18" AND returned_on IS NOT NULL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement