SQl Commands (1)
SQl Commands (1)
SHOW TABLES;
DESCRIBE movies;
****************************************************************************************************
#result-set: a set of rows that form the result of a query along with column-names and meta-data.
****************************************************************************************************
LIMIT:
****************************************************************************************************
ORDER BY:
# the output row order maynot be same as the one in the table due to query optimzier and internal data-
structres/indices.
****************************************************************************************************
DISTINCT:
# multiple-column DISTINCT
SELECT DISTINCT first_name, last_name FROM directors;
****************************************************************************************************
WHERE:
SELECT name,year,rankscore FROM movies WHERE rankscore>9 ORDER BY rankscore DESC LIMIT 20;
# "=" doesnot work with NULL, will give you an empty result-set.
SELECT name,year,rankscore FROM movies WHERE rankscore = NULL;
SELECT name,year,rankscore FROM movies WHERE rankscore IS NOT NULL LIMIT 20;
****************************************************************************************************
# LOGICAL OPERATORS: AND, OR, NOT, ALL, ANY, BETWEEN, EXISTS, IN, LIKE, SOME
SELECT name,year,rankscore FROM movies WHERE year BETWEEN 1999 AND 2000;
#inclusive: year>=1999 and year<=2000
SELECT name,year,rankscore FROM movies WHERE year BETWEEN 2000 AND 1999;
#lowvalue <= highvalue else you will get an empty result set
# If we want to macth % or _, we should use the backslash as the escape character: \% and \_
SELECT first_name, last_name FROM actors WHERE first_name LIKE 'L%' AND first_name NOT LIKE 'Li%';
****************************************************************************************************
Aggregate functions: Computes a single value on a set of rows and returns the aggreagate
****************************************************************************************************
GROUP-BY
SELECT year, COUNT(year) year_count FROM movies GROUP BY year ORDER BY year_count;
# year_count is an alias.
# often used with COUNT, MIN, MAX or SUM.
# if grouping columns contain NULL values, all null values are grouped together.
****************************************************************************************************
HAVING:
# Print years which have >1000 movies in our DB [Data Scientist for Analysis]
SELECT year, COUNT(year) year_count FROM movies GROUP BY year HAVING year_count>1000;
# specify a condition on groups using HAVING.
Order of execution:
1. GROUP BY to create groups
2. apply the AGGREGATE FUNCTION
3. Apply HAVING condition.
SELECT year, COUNT(year) year_count FROM movies WHERE rankscore>9 GROUP BY year HAVING
year_count>20;
# HAVING vs WHERE
## WHERE is applied on individual rows while HAVING is applied on groups.
## HAVING is applied after grouping while WHERE is used before grouping.
****************************************************************************************************
JOINs:
# natural join: a join where we have the same column-names across two tables.
#T1: C1, C2
#T2: C1, C3, C4
# returns C1,C2,C3,C4
# no need to use the keyword "ON"
SELECT m.name, g.genre from movies m LEFT JOIN movies_genres g ON m.id=g.movie_id LIMIT 20;
#LEFT JOIN or LEFT OUTER JOIN
#RIGHT JOIN or RIGHT OUTER JOIN
#FULL JOIN or FULL OUTER JOIN
#JOIN or INNER JOIN
#Practical note about joins: Joins can be expensive computationally when we have large tables.
****************************************************************************************************
# Syntax:
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
# first the innner query is executed and then the outer query is executed using the output values in the inner
query
# IN, NOT IN, EXISTS, NOT EXISTS, ANY, ALL, Comparison operators
#EXISTS returns true if the subquery returns one or more records or NULL
# ANY operator returns TRUE if any of the subquery values meet the condition.
# ALL operator returns TRUE if all of the subquery values meet the condition.
SELECT * FROM movies where rankscore >= ALL (SELECT MAX(rankscore) from movies);
# get all movies whose rankscore is same as the maximum rankscore.
# https://en.wikipedia.org/wiki/Correlated_subquery
****************************************************************************************************
INSERT INTO movies(id, name, year, rankscore) VALUES (412321, 'Thor', 2011, 7);
INSERT INTO movies(id, name, year, rankscore) VALUES (412321, 'Thor', 2011, 7), (412322, 'Iron Man',
2008, 7.9), (412323, 'Iron Man 2', 2010, 7);
# UPDATE Command
UPDATE <TableName> SET col1=val1, col2=val2 WHERE condition
****************************************************************************************************
#DELETE
****************************************************************************************************
# Datatypes: https://www.journaldev.com/16774/sql-data-types
# Constraints: https://www.w3schools.com/sql/sql_constraints.asp
****************************************************************************************************
****************************************************************************************************
#https://dev.mysql.com/doc/refman/8.0/en/drop-table.html
****************************************************************************************************
https://en.wikipedia.org/wiki/Data_control_language
https://dev.mysql.com/doc/refman/8.0/en/grant.html
https://dev.mysql.com/doc/refman/8.0/en/revoke.html