SAQL
SAQL
@ytyagi782
All-In-one
SQL Cheatsheet
Commands, Syntax, and
Examples for Every Scenario
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
DML Commands
Data Manipulation Language
INSERT INTO
The INSERT command table_nane INSERT INTO customers
INSERT adds new records to a (column, column2) (first_name, last_name
table. VALUES VALUES (‘Mary’, ‘Doe’);
(value1, value2);
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
DDL Commands
Data Definition Language
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
DCL Commands
Data Control Language
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
Querying Data
Commands
Command Description Syntax Example
SELECT column_name,
SELECT category, COUNT(*)
The HAVING clause filters COUNT (*) FROM
HAVING FROM products GROUP BY
grouped results based on a table_name
Clause category HAVING
specified condition. GROUP BY column_name
COUNT(*)>5;
HAVING condition;
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
Joining Commands
Command Description Syntax Example
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
Subqueries in SQL
Command Description Syntax Example
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
Aggregate Functions
Commands
Command Description Syntax Example
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
SELECT
The LEFT command returns a SELECT LEFT(string,
LEFT (product_name, 5)
LEFT() specified number of characters num_characters) AS left_string
AS left_product_name
from the left of a string. FROM table_name;
FROM products;
SELECT
The RIGHT command returns a SELECT RIGHT(string,
RIGHT (order_number, 4) AS
RIGHT() specified number of characters num_characters) AS
right_order_number FROM
from the right of a string. right_string FROM table_name ;
orders ;
SELECT
SELECT REPLACE (string,
The REPLACE command REPLACE (description, 'old_string’ ,
Old_substring , new_substring) AS
REPLACE() replaces occurrences of a
replaced_string FROM table-
'new_string') AS
substring within a string, replaced_description FROM
name;
product_descriptions ;
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
DATE_ADD Example
SELECT
DATE_ADD(‘2024-04-11’ , INTERVAL
The DATE_ADD command adds
SELECT 1 DAY) AS new_date;
DATE_ADD()/ or subtracts a specified number
DATE_ADD (date_expression ,
DATE_SUB() of days, months, or years
INTERVAL value unit) AS new_date ; DATE_SUB Example
to/from a date.
SELECT
DATE_SUB(‘2024-04-11’ INTERVAL 1
DAY) AS new_date;
SELECT
The TO_CHAR command SELECT
TO_CHAR(‘2024-04-11’,
TO_CHAR() converts a date or time to a TO_CHAR(date_expression,’format')
'YYYY-HH-DD') AS
specified format. AS formatted_date ;
formatted_date;
SELECT
The DATEDIFF command
SELECT DATEDIFF(date1, DATEDIFF(' 2024-04-11' ,
DATEDIFF() calculates the difference in days
date2) AS difference _ in _ days; '2024-04-10') AS
between two dates.
difference_in_days;
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
Conditional Expressions
Command Description Syntax Example
SELECT SELECT
column1, order_id,
column2, total_amount,
CASE CASE
The CASE statement WHEN condition1 WHEN total_amount
CASE allows you to perform THEN result1 > 1000 THEN 'High Value Order’
Statment conditional logic within a WHEN condition2 WHEN total_amount
query. THEN result2 > 500 THEN ‘Medium value
ELSE Order’ Order’
default_result ELSE 'Low Value
END AS alias END AS order_Status
FROM table_name; FROM orders;
SELECT
name,
The IF function evaluates a SELECT IF (condition,
age ,
condition and returns a true_value,
IF Statement IF(age > 58, ‘Senior’ 'Junior')
value based on the false_value) AS alias
AS
evaluation. FROM table_name;
employee-category
FROM employees;
SELECT
SELECT
The COALESCE function COALESCE(first_name,
COALESCE() COALESCE(va1ue1,
returns the first non-null middle_name) AS
Function value2 , ) AS alias
value from a list of values. preferred_name
FROM table_name;
FROM employees;
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
Set Operations
Command Description Syntax Example
SELECT column1,column
SELECT first_name, last-name
The UNION operator 2
FROM customers
combines the result sets FROM table1
UNION
UNION of two or more SELECT UNION
SELECT first_name, last _ name
statements into a single SELECT column1,
FROM employees;
result set. column2 FROM
table2;
SELECT column1,
cotumn2 FROM
SELECT first_name, last_name
The INTERSECT operator table1
FROM customers
returns the common rows INTERSECT
INTERSECT INTERSECT
that appear in both result SELECT
SELECT first_name, last-name
sets. column1, column2 FROM
FROM employees;
table2 ;
SELECT column1,
The EXCEPT operator SELECT first _ name, last_name
cotumn2 FROM
returns the distinct rows FROM customers
table1
EXCEPT from the left result set that EXCEPT
EXCEPT SELECT
are not present in the right SELECT first_name, last_name
column1, column2 FROM
result FROM employees;
table2 ;
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782
BEGIN TRANSACTION;
The COMMIT command is used to SQL statements and changes within the transaction
save all the changes made during INSERT INTO employees (name, age) VALUES ( 'Alice' , 30);
COMMIT the current transaction and make
COMMIT;
UPDATE products SET price = 25.00 WHERE category ='
them permanent, Electronics ;
COMMIT;
BEGIN TRANSACTION;
The ROLLBACK command is used SQL statements and changes within the transaction
to undo all the changes made INSERT INTO employees (name, age) VALUES ('Bob', 35)
ROLLBACK during the current transaction and
ROLLBACK;
UPDATE products SET price = 30.00 WHERE category
discard them. =‘Electronics ‘ ;
ROLLBACK;
BEGIN TRANSACTION;
INSERT INTO employees (name,age) VALUES(‘carol’, 28);
SAVEPOINT before_update;
UPDATE products SET price = 40.00 WHERE category
The SAVEPOINT command is used
SAVEPOINT =‘Electronics’ ;
SAVEPOINT to set a point within a transaction
savepoint_name; SAVEPOINT after_update;
to Which you can later roll back. DELETE FROM customers WHERE age > 60;
ROLLBACK TO before_update;
At this point, the DELETE is rolled back, UPDATE remains.
COMMIT;
BEGIN TRANSACTION;
INSERT INTO employees (name, age) VALUES (‘David’, 42);
SAVEPOINT before_update;
UPDATE products SET price = 50.00 WHERE
The ROLLBACK TO SAVEPOINT category=‘Electronics’ ;
ROLLBACK TO
ROLLBACK TO command is used to roll back to a SAVEPOINT after_update;
SAVEPOINT
DELETE FROM customers WHERE age > 60;
SAVE-POINT specific savepoint Within a
savepoint_name ;
transaction. Rollback to the savepoint before the update
ROLLBACK TO SAVEPOINT before_update;
At this point, the UPDATE is rolled back, but the INSERT
remains.
COMMIT;
BEGIN TRANSACTION;
The SET TRANSACTION command Set the isolation levet to READ COMMITTED
SET TRANSACTION SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
is used to configure properties for
SET [ISOLATION LEVEL SQL statements and Changes within the transaction
the current transaction, such as
TRANSACTION { READ COMMITTED INSERT INTO employees (name, age) VALUES (‘Emily’, 35);
isolation level and transaction UPDATE products SET price = 60.00 WHERE category
$ERIALIZABLE}]
mode. =‘Electronics ;
COMMIT;
https://www.linkedin.com/in/ytyagi782/
Yogesh Tyagi
@ytyagi782