SQL Commands
Types
- DDL - data definition language, such as CREATE, ALTER, DROP
- DQL - data query language, such as SELECT
- DML - data manipulation language, such as INSERT, UPDATE, DELETE
- DCL - data control language, such as GRANT, REVOKE
- TCL - transaction control language, such as COMMIT, ROLLBACK


QUERIES
SHOW
SHOW CREATE TABLE tbl_name
SHOW FIELDS FROM tbl_name;
SHOW CHARACTER SET;
SHOW GLOBAL VARIABLES where Variable_Name='innodb_buffer_pool_size';
AND
AND is an operator that combines two conditions. Both conditions must be true for the row to be included in the result set.
SELECT column_name(s)
FROM table_name
WHERE column_1 = value_1
AND column_2 = value_2;
AS
AS is a keyword in SQL that allows you to rename a column or table using an alias.
SELECT column_name AS 'Alias'
FROM table_name;
BETWEEN
The BETWEEN operator is used to filter the result set within a certain range. The values can be numbers, text or dates.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value_1 AND value_2;
SELECT *
FROM movies
WHERE name BETWEEN 'D%' and 'G%';
-- This will select all movies that starts with D, E, F and not G.
CASE
CASE expression is essentially the 'if/then' of the SQL world. Given one value, CASE can return another of your choice - but did you know you're able to use it in ORDER BY clauses to define custom orders based upon other values?
Here's a simple use case. Let's say you have a table listing employees and their job titles:
| id | name | title |
|---|---|---|
| 1 | Oscar | Cleaner |
| 2 | Carol | CEO |
| 3 | Jimbo | CFO |
| 4 | Bobby | Assistant |
If you wanted to have some sort of order based upon job titles, you could use CASE like so:
SELECT * FROM employees ORDER BY
CASE
WHEN title = 'CEO' THEN 1
WHEN title = 'CFO' THEN 2
WHEN title = 'CTO' THEN 2
ELSE 3
END;
-- (Work in MS SQL Server not in Oracle)
SELECT name,
CASE
WHEN genre = 'romance' THEN 'Chill'
WHEN genre = 'comedy' THEN 'Chill'
ELSE 'Intense'
END AS 'Mood'
FROM movies;
SELECT
CASE
WHEN Grades.Grade < 8 THEN NULL
ELSE Students.Name
END AS 'Student_Name',
Grades.Grade, Students.Marks
FROM Students
JOIN Grades
ON Students.Marks BETWEEN Grades.Min_Mark and Grades.Max_Mark
ORDER BY Grades.Grade DESC, Students.Name, Students.Marks;
HAVING
HAVING statement always comes after GROUP BY, but before ORDER BY and LIMIT.
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
IN
SELECT column_name(s)
FROM table_name
WHERE column_name
IN (value1,value2,..)
OR
OR is an operator that filters the result set to only include rows where either condition is true.
SELECT column_name
FROM table_name
WHERE column_name = value_1
OR column_name = value_2;
ORDER BY
ORDER BY is a clause that indicates you want to sort the result set by a particular column either alphabetically or numerically.
SELECT column_name
FROM table_name
ORDER BY column_name ASC|DESC;
SELECT Name
FROM STUDENTS
WHERE Marks > 75
ORDER BY RIGHT(NAME, 3), ID ASC;
ORDER BY ALL
A shorthand equivalent to specifying all expressions in the SELECT list in the order they occur. If sort_direction or nulls_sort_order are specified they apply to each expression.
ORDER BY clause | Databricks on AWS
LIKE
LIKE is a special operator used with the WHERE clause to search for a specific pattern in a column.
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
SELECT * FROM movies WHERE name LIKE 'Se_en';
-- MY SQL
SELECT DISTINCT CITY
FROM STATION
WHERE
CITY LIKE 'a%' OR
CITY LIKE 'e%' OR
CITY LIKE 'i%' OR
CITY LIKE 'o%' OR
CITY LIKE 'u%';
-- MS SQL SERVER
SELECT DISTINCT CITY
FROM STATION
WHERE
CITY LIKE '%[aeiou]';
-- Here _ in a wildcard for substituting any value % is a wildcard character that matches zero or more missing letters in the pattern
LIKE is an operator that compares column values with a specified pattern. During pattern matching, regular characters must exactly match the characters specified in the character string. The data type of the column can be any character or date data type. There are certain characters within the pattern, called wildcard characters.
I have used four types of wildcards; they are:
- Percent sign
(%): It is used to represent or search any string of zero or more characters. - Underscore
(_): It is used to represent or search a single character. - Bracket
([]): It is used to represent or search any single character within the specified range. - Caret
(^): It is used to represent or search any single character not within the specified range.
SELECT User_ID, FirstName, LastName, Salary FROM UserDetail WHERE FirstName LIKE '%h%';
LIKE BINARY
The BINARY function converts a value to a binary string.
LIKE BINARY is used for case insensitive search
LIMIT
LIMIT is a clause that lets you specify the maximum number of rows the result set will have.
SELECT column_name(s)
FROM table_name
LIMIT number;