Guest User

Untitled

a guest
Nov 2nd, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. == SQL Snippets ==
  2.  
  3. -- Lesson1
  4.  
  5. -- show all
  6. SELECT * FROM {{table_name}};
  7. -- select more then 1 column
  8. SELECT name, imdb_rating FROM movies;
  9.  
  10. -- create table
  11. CREATE TABLE celebs (id INTEGER, name TEXT, age INTEGER);
  12.  
  13. -- insert into table
  14. INSERT INTO celebs (id, name, age) VALUES (1, 'test', 21);
  15.  
  16. -- show value from table
  17. SELECT name FROM celebs;
  18.  
  19. -- update VALUES
  20. UPDATE celebs SET age = 22 WHERE id = 1;
  21.  
  22. -- adding new columns to the table, changes an existing table.
  23. ALTER TABLE celebs ADD COLUMN twitter_handle TEXT;
  24.  
  25. -- Delete all of the rows that have a NULL value in the twitter column. Above SELECT type
  26. DELETE FROM celebs WHERE twitter_handle IS NULL;
  27.  
  28. -- sofar
  29. CREATE TABLE creates a new table.
  30. INSERT INTO adds a new row to a table.
  31. SELECT queries data from a table.
  32. UPDATE edits a row in a table.
  33. ALTER TABLE changes an existing table.
  34. DELETE FROM deletes rows from a table.
  35.  
  36. -- Lesson2
  37.  
  38. -- SELECT DISTINCT is used to return unique values in the result set. It filters out all duplicate values. Here, the result set lists each genre in the movies table exactly once.
  39. SELECT DISTINCT genre FROM movies;
  40.  
  41. -- select by filter column
  42. SELECT * FROM movies WHERE imdb_rating > 8;
  43. filters: = equals, != not equals, > greater than, < less than, >= greater than or equal to, <= less than or equal to
  44.  
  45. -- Like
  46. SELECT * FROM movies WHERE name LIKE 'Se_en'; -- SeVen, Se7en
  47. SELECT * FROM movies WHERE name LIKE '%man%'; -- spider-man, iron man, man of steel
  48. SELECT * FROM movies WHERE name LIKE 'a%'; -- everything starts with an 'a'
  49. LIKE is a special operator used with the WHERE clause to search for a specific pattern in a column.
  50. A% matches all movies with names that begin with "A"
  51. %a matches all movies that end with "a"
  52.  
  53. -- another examples with BETWEEN
  54. SELECT * FROM movies WHERE name BETWEEN 'A' AND 'J'; -- name starts betwen a to j
  55. SELECT * FROM movies WHERE year BETWEEN 1990 AND 2000; -- year between 1990 and 2000
  56. -- filter between years and genre
  57. SELECT * FROM movies WHERE year BETWEEN 1990 AND 2000 AND genre = 'comedy';
  58.  
  59. -- AND
  60. AND is an operator that combines two conditions. Both conditions must be true for the row to be included in the result set.
  61. Here, we use the AND operator to only return movies made between 1990 and 2000 that are also comedies.
  62. -- OR
  63. The OR operator evaluates each condition separately and if any of the conditions are true then the row is added to the result set. example:
  64. SELECT * FROM movies WHERE genre = 'comedy' OR year < 1980;
  65.  
  66. -- DESC /ASC order by
  67. ORDER BY is a clause that indicates you want to sort the result set by a particular column either alphabetically or numerically. example:
  68. SELECT * FROM movies ORDER BY imdb_rating DESC;
  69. SELECT * FROM movies ORDER BY imdb_rating ASC;
  70.  
  71. -- LIMIT
  72. LIMIT is a clause that lets you specify the maximum number of rows the result set will have. Here, we specify that the result set can not have more than three rows.
  73.  
  74. -- sofar
  75. SELECT is the clause you use every time you want to query information from a database.
  76. WHERE is a popular command that lets you filter the results of the query based on conditions that you specify.
  77. LIKE and BETWEEN are special operators that can be used in a WHERE clause
  78. AND and OR are special operators that you can use with WHERE to filter the query on two or more conditions.
  79. ORDER BY lets you sort the results of the query in either ascending or descending order.
  80. LIMIT lets you specify the maximum number of rows that the query will return. This is especially important in large tables that have thousands or even millions of rows.
  81.  
  82. -- Lesson3
  83. SELECT COUNT(*) FROM fake_apps;
  84. COUNT() is a function that takes the name of a column as an argument and counts the number of rows where the column is not NULL. Here, we want to count every row so we pass * as an argument.
  85.  
  86. -- show the group prices and their counts
  87. SELECT price, COUNT(*) FROM fake_apps GROUP BY price;
  88. GROUP BY is a clause in SQL that is only used with aggregate functions. It is used in collaboration with the SELECT statement to arrange identical data into groups.
  89.  
  90. -- SUM
  91. SUM() is a function that takes the name of a column as an argument and returns the sum of all the values in that column. Here, it adds all the values in the downloads column.
  92.  
  93. SELECT category, SUM(downloads) FROM fake_apps GROUP BY category; --group by
  94. SELECT MAX(downloads) FROM fake_apps; --get max value
  95.  
  96. -- MIN/MAX
  97. Return the names of the most downloaded apps in each category.
  98. SELECT name, category, MAX(downloads) FROM fake_apps GROUP BY category;
  99.  
  100. -- AVG
  101. SELECT AVG(downloads) FROM fake_apps;
  102. The AVG() function works by taking a column name as an argument and returns the average value for that column.
  103.  
  104. -- ROUND
  105. ROUND() is a function that takes a column name and an integer as an argument. It rounds the values in the column to the number of decimal places specified by the integer.
  106. Here, we pass the column AVG(downloads) and 2 as arguments. SQL first calculates the average for each price and then rounds the result to two decimal places in the result set. example:
  107. SELECT price, ROUND(AVG(downloads), 2) FROM fake_apps GROUP BY price;
  108.  
  109. -- TLDR
  110. COUNT takes the name of a column(s) as an argument and counts the number of rows where the value(s) is not NULL.
  111. GROUP BY is a clause used with aggregate functions to combine data from one or more columns.
  112. SUM() takes the column name as an argument and returns the sum of all the values in that column.
  113. MAX() takes the column name as an argument and returns the largest value in that column.
  114. MIN() takes the column name as an argument and returns the smallest value in that column.
  115. AVG() takes a column name as an argument and returns the average value for that column.
  116. ROUND() takes two arguments, a column name and the number of decimal places to round the values in that column.
  117.  
  118. -- Lesson4
  119.  
  120. -- select values from diff tables
  121. SELECT albums.name, albums.year, artists.name FROM albums, artists;
  122.  
  123. -- JOIN
  124. -- An inner join will combine rows from different tables if the join condition is true
  125. SELECT * FROM albums JOIN artists ON albums.artist_id = artists.id;
  126.  
  127. -- AS
  128. AS is a keyword in SQL that allows you to rename a column or table using an alias
  129. SELECT albums.name AS 'Album', albums.year, artists.name AS 'Artist' FROM albums JOIN artists ON albums.artist_id = artists.id WHERE albums.year > 1980;
  130.  
  131. -- TLDR
  132. Primary Key is a column that serves a unique identifier for row in the table. Values in this column must be unique and cannot be NULL.
  133. Foreign Key is a column that contains the primary key to another table in the database. It is used to identify a particular row in the referenced table.
  134. Joins are used in SQL to combine data from multiple tables.
  135. INNER JOIN will combine rows from different tables if the join condition is true.
  136. LEFT OUTER JOIN will return every row in the left table, and if the join condition is not met, NULL values are used to fill in the columns from the right table.
  137. AS is a keyword in SQL that allows you to rename a column or table in the result set using an alias.
Add Comment
Please, Sign In to add comment