Please enable JavaScript.
Coggle requires JavaScript to display documents.
SQL - Coggle Diagram
SQL
Concepts
Database is like folder containing multiple tables
databases cant be nested, use common prefixes instead, as some gui group together databases with common prefix
keywords
-
ADD CONSTRAINT -- Creates a new constraint on an existing table, which is used to specify rules for any data in the table.
ALTER TABLE -- Adds, deletes or edits columns in a table. It can also be used to add and delete constraints in a table, as per the above.
-
-
-
-
AS -- Renames a table or column with an alias value which only exists for the duration of the query.
-
-
-
-
-
-
-
-
-
-
-
-
-
EXISTS -- Checks for the existence of any record within the subquery, returning true if one or more records are returned.
-
-
-
-
-
-
-
-
ORDER BY -- Used to sort the result data in ascending (default) or descending order through the use of ASC or DESC keywords.
-
SELECT -- Used to select data from a database, which is then returned in a results set.
SELECT DISTINCT -- Sames as SELECT, except duplicate values are excluded.
-
-
-
-
-
TRUNCATE TABLE -- Similar to DROP, but instead of deleting the table and its data, this deletes only the data.
-
UNION ALL -- The same as UNION, but includes duplicate values.
-
-
-
-
Operators
- Arithmetic Operators in SQL
-
-
-
-
-
-
-
-
-
- Comparison Operators in SQL
-
-
-
= -- Greater than or equal to
-
-
- Compound Operators in SQL
-
-
-
-
-
-
-
-
-
-
-
Joins
-
-
types
inner - in mysql it is implicit, i.e. simply writing join will give inner join, returns rows for which key is present in both,
e.x. select * from dept join emp on dept.deptno=emp.deptno;
-
left join - will give result of inner join + any rows in left with key value which was not in right.
e.x. select * from dept left join emp on dept.deptno=emp.deptno;
right join - will give resutlt of inner join + any rows in right with key value which was not in left. e.x. select * from dept right join emp on dept.deptno=emp.deptno;
-
full join - mysql does not suppor full outer join, it is implemented using 'union' between left join and right join. in case 'union all' is used. one of the left or right joins must be anti join. this is because union gives distinct values, where as union all gives duplicates as well.
self join - when there is self relation in a table, for example when a table contains employees of an hierarchical organization. with column containing employee id of managers of that employee. e.x. select e.empno, m.empno as mgrid from emp e inner join emp m on e.mgr=m.empno;
Comments : 1. Single line - use -- e.x. --select from tablename, 2. Multi line -use / ***/
just like other languages
-
-
SQL Categories
-
-
DCL
Grant
GRANT ALL PRIVILEGES ON database/database.tablename to name/name@host;
GRANT CLAUSE1, CLAUSE2, CLAUSE3 ON database/database.tablename to name/name@host;
Revoke
REVOKE ALL PRIVILEGES / CLAUSE1, CLAUSE2 ... .. ON database/database.table from name/name@host
-
-
-
to be added :
-
-
concepts like partition, order by, in, between, index, view, plsql
-
-
Functions
String
-
-
-
CONCAT -- Adds expressions together, with a minimum of 2.
CONCAT_WS -- Adds expressions together, but with a separator between each value.
-
-
FORMAT -- When passed a number, returns that number formatted to include commas (eg 3,400,000).
INSERT -- Allows you to insert one string into another at a certain point, for a certain number of characters.
-
-
LEFT -- Starting from the left, extracts the given number of characters from a string and returns them as another.
LENGTH -- Returns the length of a string, but in bytes.
LOCATE -- Returns the first occurrence of one string within another,
-
LPAD -- Left pads one string with another, to a specific length.
-
MID -- Extracts one string from another, starting from any position.
-
-
REPLACE -- Allows you to replace any instances of a substring within a string, with a new substring.
-
RIGHT -- Starting from the right, extracts the given number of characters from a string and returns them as another.
RPAD -- Right pads one string with another, to a specific length.
-
-
-
SUBSTR -- Extracts one substring from another, starting from any position.
-
SUBSTRING_INDEX -- Returns a substring from a string before the passed substring is found the number of times equals to the passed number.
TRIM -- Removes trailing and leading spaces from the given string. Same as if you were to run LTRIM and RTRIM together.
-
-
Numeric
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
LOG -- Returns the natural logarithm of the given number, or the logarithm of the given number to the given base.
LOG10 -- Does the same as LOG, but to base 10.
LOG2 -- Does the same as LOG, but to base 2.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Date
ADDDATE -- Adds a date interval (eg: 10 DAY) to a date (eg: 20/01/20) and returns the result (eg: 20/01/30).
ADDTIME -- Adds a time interval (eg: 02:00) to a time or datetime (05:00) and returns the result (07:00).
-
-
-
-
-
-
-
-
-
DATE_SUB -- Subtracts a date interval (eg: 10 DAY) to a date (eg: 20/01/20) and returns the result (eg: 20/01/10).
-
-
-
-
-
-
-
-
-
-
MAKEDATE -- Creates a date and returns it, based on the given year and number of days values.
MAKETIME -- Creates a time and returns it, based on the given hour, minute and second values.
-
-
-
-
-
-
-
-
-
-
-
-
SUBTIME -- Subtracts a time interval (eg: 02:00) to a time or datetime (05:00) and returns the result (03:00).
-
-
-
-
-
-
-
-
-
-
-
-
Misc
-
-
-
COALESCE -- From a list of values, returns the first non-null value.
CONNECTION_ID -- For the current connection, returns the unique connection ID.
-
-
-
-
GROUP BY -- Used alongside aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the results.
-
IF -- If the condition is true it returns a value, otherwise it returns another value.
IFNULL -- If the given expression equates to null, it returns the given value.
ISNULL -- If the expression is null, it returns 1, otherwise returns 0.
LAST_INSERT_ID -- For the last row which was added or updated in a table, returns the auto increment ID.
NULLIF -- Compares the 2 given expressions. If they are equal, NULL is returned, otherwise the first expression is returned.
-
-
-
-
Resources, except the obvious ones.like sql documentation page, w3 school, gfg.
-
wikipedia page for sql and sql syntax contain complete list of keywords(which even sql documentation does not have)
-