IF function
The IF function is used to evaluate an expression and return a value.
The IF function consists of three parts.
1. condition to evaluate.
2. expression(can be another IF function) or value if the condition is evaluated to true.
3. expression(can be another IF function) or value if the condition is evaluated to false.
IF(condition, if_true_expr_or_value, if_false_expr_or_value)
Our example above shows ‘no sales rep’ if the condition(sales_rep_id IS NULL) is true and ‘sales rep’ if false.
IF function with aggregate functions
Here we are summing up the number of customers with and without reps.
Stored procedures are SQL statements that you can save into a database and reuse over and over again.
Syntax to create a stored procedure.
DELIMITER // CREATE PROCEDURE procedureName() BEGIN SELECT * FROM table_name; END // DELIMITER ;
To execute a store procedure, call its name.
CALL procedureName();
Advantages of Stored Procedures
Disadvantages of Stored Procedures
Stored Procedure Variables
Variables are local variables that hold values just for the execution of the stored procedure.
DECLARE variable_name datatype[INT,VARCHAR,DATETIME,...](size) DEFAULT default_value;
DECLARE total_count INT DEFAULT 0; SELECT COUNT(*) INTO total_count FROM table_name;
Variable scope
– variables that start with @
are session variables which go out of scope when the session end.
– variables go out of scope depending on where there are at.
Stored Procedure Parameters
Almost all stored procedures require parameters or input from the applications that call the stored procedures.
A stored procedure parameters have 3 modes – IN, OUT, INOUT
IN – parameters passed from the applications. IN parameters are protected which means that the stored procedure copies the values of the parameters and not modifies the real parameters.
DELIMITER // CREATE PROCEDURE prodecureName(IN parameterName VARCHAR(255)) BEGIN SELECT * FROM table_name WHERE column_name = parameterName; END // DELIMITER ;
CALL prodecureName('parameterName');
OUT – parameters passed as results to the applications.
DELIMITER $$ CREATE PROCEDURE procedureName( IN parameter1 VARCHAR(25), OUT parameterX INT) BEGIN SELECT count(*) INTO parameter1 FROM table_name WHERE parameter_name = parameter1; END$$ DELIMITER ;
CALL procedureName('parameter1',@total); SELECT @total;
INOUT – parameters can be changed and passed back to the calling applications.
DELIMITER $$ CREATE PROCEDURE add_name(INOUT name VARCHAR(25),IN newName VARCHAR(25)) BEGIN SET name = CONCAT(name," ",newName); END$$ DELIMITER ;
SET @name = 'Folau'; - Folau CALL add_name(@name,'Lisa'); -- Folau Lisa CALL add_name(@name,'Kinga'); -- Folau Lisa Kinga CALL add_name(@name,'Fusi'); -- Folau Lisa Kinga Fusi SELECT @name; -- Folau Lisa Kinga Fusi
Return Multiple Values from Stored Procedures
DELIMITER $$ CREATE PROCEDURE getCounts( IN userType INT, OUT parameter1 INT, OUT parameter2 INT) BEGIN -- new SELECT count(*) INTO parameter1 FROM users WHERE user_type = userType AND status = 'new'; -- deleted SELECT count(*) INTO parameter2 FROM users WHERE user_type = userType AND status = 'deleted'; END
CALL getCounts('soldier', @new, @deleted); SELECT @new, @deleted;
Sometimes you want to run updates using a store precedure and then drop it right after
DELIMITER $$ CREATE DEFINER=`root`@`localhost` PROCEDURE `sale_update`() NO SQL BEGIN DECLARE newPrice INT DEFAULT FALSE; DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN SHOW ERRORS; ROLLBACK; END; START TRANSACTION; -- transaction start INSERT INTO `sale`(`member_uuid`, `product_id`, `price`) VALUES ('mem_sadfljlweruoasd', 12, 250); INSERT INTO `sale`(`member_uuid`, `product_id`, `price`) VALUES ('mem_sadfljlweruoasd', 23, 12); -- transaction end COMMIT WORK; END$$ DELIMITER ; -- RUN SP: sale_update CALL sale_update(); -- DROP SP: sale_update DROP PROCEDURE IF EXISTS sale_update;
The GROUP BY clause is used to group sets of rows into groups and returns only one row from each group. It is mostly used with aggregate functions but it does not have to.
SELECT column_name1,column_name2,aggregate_function(column_name3),... FROM table_name WHERE conditions GROUP BY column_name3;
sdfsd
GROUP BY with aggregate functions
AVG – average value of a column
SUM – sum value of a column
MAX- maximum value of a column
MIN – minimum value of a column
COUNT – count how many in the group
Aggregate functions perform some calculation on each group formed by GROUP BY and return a single value.
How to get number of vendors in each vendor group?
GROUP BY with expression
GROUP BY with HAVING
HAVING is used to filter the groups formed by GROUP BY. WHERE filters each row in the result set. HAVING filters each group in the groups formed by GROUP BY.
Applying HAVING with a criteria of average price of less than 1000.
If GROUP BY is omitted then HAVING is bahaving like the WHERE clause.
HAVING and GROUP BY can be used together to query statistical data about your data like finding the average order or the average number of dollars spent by a certain group of customers.
GROUP BY with ROLL UP
ROLL UP is used with GROUP BY to calculate subtotals and the grand total for groups form by GROUP BY.
Snowflake’s Data Cloud is powered by an advanced data platform provided as Software-as-a-Service (SaaS). Snowflake enables data storage, processing, and analytic solutions that are faster, easier to use, and far more flexible than traditional offerings.
The Snowflake data platform is not built on any existing database technology or “big data” software platforms such as Hadoop. Instead, Snowflake combines a completely new SQL query engine with an innovative architecture natively designed for the cloud. To the user, Snowflake provides all of the functionality of an enterprise analytic database, along with many additional special features and unique capabilities.
Snowflake provides a single platform for data warehousing, data lakes, data engineering, data science, data application development, and secure sharing and consumption of real-time / shared data. Snowflake features out-of-the-box features like separation of storage and compute, on-the-fly scalable compute, data sharing, data cloning, and third-party tools support in order to handle the demanding needs of growing enterprises.
Benefits of Snowflake