{"id":1948,"date":"2022-06-17T10:15:38","date_gmt":"2022-06-17T05:15:38","guid":{"rendered":"https:\/\/testing.dicecamp.com\/insights\/?p=1948"},"modified":"2022-06-21T10:38:38","modified_gmt":"2022-06-21T05:38:38","slug":"6-sql-statements-for-advance-data-analytics-advance-sql","status":"publish","type":"post","link":"https:\/\/testing.dicecamp.com\/insights\/6-sql-statements-for-advance-data-analytics-advance-sql\/","title":{"rendered":"6 advance SQL statements for Data Analytics (Advance SQL)"},"content":{"rendered":"\n<p>SQL is by far the most widely accepted programming language across the data analytics industry. The 40 year old language is well documented and is very easy to learn.&nbsp;<\/p>\n\n\n\n<p>In this article, we look at 6 advance SQL statements with examples that will help you to manipulate data in a more flexible and powerful way.&nbsp;&nbsp;<\/p>\n\n\n\n<p><strong>IMPORTANT<\/strong>: All examples provided in this article have been tested on our custom database \u2018Dice\u2019 that consists of two tables; \u2018Employee\u2019 and \u2018Department\u2019. These tables are shown as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized td-caption-align-center\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-9-1024x693.png\" alt=\"\" class=\"wp-image-2016\" width=\"512\" height=\"347\" srcset=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-9-1024x693.png 1024w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-9-300x203.png 300w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-9-768x519.png 768w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-9-1536x1039.png 1536w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-9-150x101.png 150w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-9-696x471.png 696w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-9-1068x722.png 1068w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-9.png 1588w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><figcaption><em>Database used for examples: There are two tables; Employee (TOP), and Department (Bottom)<\/em><\/figcaption><\/figure>\n<\/div>\n\n\n<p>Now let&#8217;s see the SQL statements one by one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Table of Contents<\/h2>\n\n\n\n<ol class=\"wp-block-list\" id=\"myheading1\"><li><a href=\"#myheading1\">SQL JOIN<\/a><\/li><li><a href=\"#anchor-2\">SQL Aggregate Functions<\/a><\/li><li><a href=\"#anchor-3\">SQL GROUP BY<\/a><\/li><li><a href=\"#anchor-4\">SQL OVER ()<\/a><\/li><li><a href=\"#anchor-5\">SQL PARTITION BY<\/a><\/li><li><a href=\"#anchor-6\">SQL ORDER BY<\/a><\/li><\/ol>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"myheading1\"> SQL JOIN<\/h1>\n\n\n\n<p>Oftentimes while manipulating data we want to combine information from two tables. This is done using SQL JOIN statements.&nbsp;&nbsp;<\/p>\n\n\n\n<p>For two tables to join they must contain a common column.&nbsp;<\/p>\n\n\n\n<p><strong>Practical Tip<\/strong>: In the modeling stage of a database, a data modeler puts a unique column of one table (the primary key; pk) in the other table (a foreign key; fk) to enable join in the later stage of data engineering.&nbsp;<\/p>\n\n\n\n<p>There are a number of ways we can achieve \u2018join\u2019 operation between tables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">INNER JOIN<\/h2>\n\n\n\n<p>Inner join is by far the most simple join and combines two tables while displaying only the rows that match as per the joining criteria.&nbsp;<\/p>\n\n\n\n<p>All unmatched values are discarded in the inner join.<\/p>\n\n\n\n<p>Inner join works by sequentially matching a row of one table with all rows of the other table as per the joining criteria.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong> INNER JOIN <em>column_name<\/em><\/p>\n\n\n\n<p>ON table_1.pk = table_2.fk;<\/p>\n\n\n\n<p><em>Where the ON<\/em> clause represents the joining criteria (often referred to as the stitching criteria) and specifies those columns that are to be matched in both tables. Here the columns are pk (primary key) and fk (foreign key) residing in both tables respectively.<\/p>\n\n\n\n<p>As an application of inner join, consider the following example.&nbsp;<\/p>\n\n\n\n<p>We want to filter employees from the employee table whose departments demonstrated excellent performance. We have a department table that carries the highest performing department names.&nbsp;<\/p>\n\n\n\n<p>We can use inner joins to filter out the employees from the employee table using the following query.<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>SELECT <\/strong>d.dept_name, e.employee_name, e.dept_id<strong>&nbsp;<\/strong><\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>FROM <\/strong>employee e<strong>&nbsp;<\/strong><\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>JOIN <\/strong>department d<\/p>\n\n\n\n<p><strong>ON <\/strong>e.dept_id = d.dept_id;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized td-caption-align-center\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-1.png\" alt=\"\" class=\"wp-image-2018\" width=\"361\" height=\"234\" srcset=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-1.png 1003w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-1-300x195.png 300w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-1-768x499.png 768w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-1-150x98.png 150w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-1-696x452.png 696w\" sizes=\"auto, (max-width: 361px) 100vw, 361px\" \/><\/figure>\n<\/div>\n\n\n<p style=\"margin-top:25px\"><em>Here<\/em> \u2018dept_id\u2019 is the primary key in &#8217;employee&#8217; and resides as foreign key in &#8216;department&#8217;. <\/p>\n\n\n\n<p><strong>Now, what if a data analyst wants to view the rest of the employees from other departments alongside as well?&nbsp;<\/strong><\/p>\n\n\n\n<p>SQL Left joins come in handy for this!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">LEFT JOIN\/RIGHT JOIN<\/h2>\n\n\n\n<p>A left join will output all rows of the first table (left table) but only matched values from the second table (as per the joining criteria).<\/p>\n\n\n\n<p>In the resulting table you might see \u2018null values\u2019 that specify the unincluded values from the second table.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong> LEFT JOIN <em>column_name<\/em>\/ RIGHT JOIN <em>column_name<\/em><\/p>\n\n\n\n<p>ON table_1.pk = table_2.fk;<\/p>\n\n\n\n<p>We use the following query to view both; employees from the highest performing department and the rest of the employees in the employee table.<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>SELECT<\/strong> *<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>FROM<\/strong> employee e&nbsp;<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>LEFT<\/strong> <strong>JOIN<\/strong> department d<\/p>\n\n\n\n<p><strong>ON<\/strong> e.dept_id = d.dept_id<strong>;<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-3-1024x470.png\" alt=\"SQL left join\" class=\"wp-image-2020\" width=\"512\" height=\"235\" srcset=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-3-1024x470.png 1024w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-3-300x138.png 300w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-3-768x352.png 768w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-3-150x69.png 150w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-3-696x319.png 696w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-3-1068x490.png 1068w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-3.png 1513w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n<\/div>\n\n\n<p style=\"margin-top:25px\"><strong>IMPORTANT: <\/strong>Total rows in left join = total matched rows + (unmatched rows from first table)<\/p>\n\n\n\n<p>Similarly, a right join will keep all rows from the second table (right table) but will only keep matched values from the first table.<\/p>\n\n\n\n<p>Total rows in right join = total matched rows + (unmatched rows from second table).&nbsp;<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>SELECT<\/strong> *<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>FROM<\/strong> employee e&nbsp;<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>RIGHT<\/strong> <strong>JOIN<\/strong> department d<\/p>\n\n\n\n<p><strong>ON<\/strong> e.dept_id = d.dept_id<strong>;<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-2-1024x446.png\" alt=\"SQL Right join\" class=\"wp-image-2021\" width=\"512\" height=\"223\" srcset=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-2-1024x446.png 1024w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-2-300x131.png 300w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-2-768x334.png 768w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-2.png 1595w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">CROSS JOIN<\/h2>\n\n\n\n<p>Cross join is a \u2018notorious\u2019 operation. It takes a lot of computational power and memory to generate and store output. We avoid its use in practical scenarios.<\/p>\n\n\n\n<p>A cross join simply combines each row of one table with all rows of the other table (similar to a \u2018combination operation\u2019 in mathematics). There\u2019s no stitching criteria because we are simply making combinations between two tables.&nbsp;<\/p>\n\n\n\n<p>The resulting table is a huge table that carries all possible combinations of data elements.&nbsp;<\/p>\n\n\n\n<p><strong>Syntax:<\/strong> CROSS JOIN <em>column_name<\/em>.<\/p>\n\n\n\n<p>In our database, the employee table has a total of 8 rows and the dept table carries 4 rows. The cross join operation results in the number of rows = 8&#215;4 =32.<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>SELECT<\/strong> *<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>FROM<\/strong> employee e&nbsp;<\/p>\n\n\n\n<p><strong>CROSS<\/strong> <strong>JOIN<\/strong> department d<strong>;<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-8-1024x799.png\" alt=\"SQL cross join\" class=\"wp-image-2022\" width=\"512\" height=\"400\" srcset=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-8-1024x799.png 1024w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-8-300x234.png 300w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-8-768x599.png 768w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-8-150x117.png 150w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-8.png 1376w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n<\/div>\n\n\n<h1 class=\"wp-block-heading\" id=\"anchor-2\">SQL Aggregate Functions<\/h1>\n\n\n\n<p>SQL aggregate functions perform mathematical operations on data.<\/p>\n\n\n\n<p>There are five number of aggregate functions; COUNT(), SUM(), MAX(), MIN(), and AVG().&nbsp;<\/p>\n\n\n\n<p><strong>Syntax:<\/strong> SELECT aggregate function(<em>column_name<\/em>) FROM <em>table_name<\/em><\/p>\n\n\n\n<p>Aggregate functions are used within the SELECT statement.<\/p>\n\n\n\n<p>It\u2019s worth noting here, each aggregate function outputs a single value against the whole column. Due to this particular reason we can\u2019t query a column when using an aggregate function.&nbsp;<\/p>\n\n\n\n<p>As we would see later in the article, the window aggregate functions are more useful when querying columns alongside aggregate results.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"anchor-3\">SQL GROUP BY<\/h1>\n\n\n\n<p>It\u2019s an immensely useful function that offers frequent support in the daily operations of a data analyst.&nbsp;<\/p>\n\n\n\n<p>GROUP BY arranges identical data of a column into groups and is used in combination with SELECT clause and Aggregate functions.&nbsp;<\/p>\n\n\n\n<p><strong>Syntax:<\/strong> GROUP BY(<em>column_name<\/em>).<\/p>\n\n\n\n<p>Use GROUP BY on a column to group similar data and then apply aggregate functions over another column to aggregate the data within each group.<\/p>\n\n\n\n<p>For example, we want to view the average salary within each department.<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>SELECT <\/strong>position_<strong>, AVG(<\/strong>salary<strong>) FROM <\/strong>employee<\/p>\n\n\n\n<p><strong>GROUP BY(<\/strong>position_<strong>);<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-4.png\" alt=\"\" class=\"wp-image-2025\" width=\"242\" height=\"150\" srcset=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-4.png 483w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-4-300x186.png 300w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-4-150x93.png 150w\" sizes=\"auto, (max-width: 242px) 100vw, 242px\" \/><\/figure>\n<\/div>\n\n\n<p style=\"margin-top:25px\">The position_ column is grouped and reduced to two rows, and for each group an average function is applied.<\/p>\n\n\n\n<p><strong>Practical tip:<\/strong> Use GROUP BY after WHERE clause but before ORDER BY or LIMIT.<\/p>\n\n\n\n<p><strong>Disadvantage:<\/strong> GROUP BY reduces the number of rows in the resulting table and therefore you cannot SELECT original columns of the table when you apply GROUP BY.<\/p>\n\n\n\n<p>This drawback of GROUP BY leads us to the \u2018window functions in SQL\u2019; OVER().&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"anchor-4\">SQL OVER()<\/h1>\n\n\n\n<p>A window aggregate function is defined as \u201cthe one which allows aggregate functions to output a result against each window (row) \u201d.&nbsp;<\/p>\n\n\n\n<p>OVER() is a useful window function and commonly used with aggregate functions. It offers the flexibility to display the aggregated results against each row of the original table.&nbsp;<\/p>\n\n\n\n<p><strong>Syntax:<\/strong> SELECT aggregate function(<em>column_name<\/em>) OVER() FROM <em>table_name<\/em><\/p>\n\n\n\n<p>Note that OVER() is used within the SELECT clause.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p style=\"max-width:1062px;margin-bottom:0px\"><strong>SELECT<\/strong> employee_name, <strong>SUM<\/strong>(salary) <strong>OVER()&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>FROM<\/strong> employee;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-5.png\" alt=\"SQL OVER\" class=\"wp-image-2026\" width=\"351\" height=\"347\" srcset=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-5.png 702w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-5-300x297.png 300w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-5-150x148.png 150w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-5-696x688.png 696w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/figure>\n<\/div>\n\n\n<p>Note the result table contains the same rows as the original table.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"anchor-5\">SQL PARTITION BY<\/h1>\n\n\n\n<p>PARTITION BY is a sub clause of OVER() function and it\u2019s immensely useful as you will witness shortly.&nbsp;<\/p>\n\n\n\n<p><strong>Syntax:<\/strong> SELECT aggregate function(<em>column_name<\/em>) OVER(PARTITION BY <em>column_name<\/em>) FROM table_name<\/p>\n\n\n\n<p><em>OVER( PARTITION BY column_name)<\/em> is like a <em>GROUP BY column_name<\/em> that groups similar data in a column. The difference lies in that now you can query multiple columns in the SELECT clause which was not earlier possible with GROUP BY.&nbsp;<\/p>\n\n\n\n<p>The result table contains the same rows as the original table.<\/p>\n\n\n\n<p>For example, I want to see how many employees are at Managers position and AM position. The following query will do it.<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>SELECT<\/strong> employee_name, position_, <strong>COUNT<\/strong>(employee_name) <strong>OVER<\/strong>(<strong>PARTITION<\/strong> <strong>BY<\/strong> position_)&nbsp;<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>FROM<\/strong> employee<\/p>\n\n\n\n<p><strong>WHERE<\/strong> dept_id <strong>IN<\/strong> (<strong>SELECT<\/strong> dept_id <strong>FROM<\/strong> department)<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-6-1-1024x557.png\" alt=\"\" class=\"wp-image-2031\" width=\"512\" height=\"279\" srcset=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-6-1-1024x557.png 1024w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-6-1-300x163.png 300w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-6-1-768x418.png 768w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-6-1-150x82.png 150w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-6-1-696x379.png 696w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-6-1-1068x581.png 1068w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-6-1.png 1121w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n<\/div>\n\n\n<p style=\"margin-top:25px\">As you can see in the results table, there are a total of 2 employees at AM position while the total employees at the Manager position are 4. The result is displayed against each row.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"anchor-6\">SQL ORDER BY<\/h1>\n\n\n\n<p>ORDER BY is a powerful clause in SQL which can be applied to more than one column. It\u2019s also used as a sub clause in the OVER()&nbsp; function.<\/p>\n\n\n\n<p style=\"margin-bottom:0px\"><strong>Syntax<\/strong>: SELECT <em>column_1, column_2, &#8230;<\/em><\/p>\n\n\n\n<p style=\"margin-bottom:0px\">              FROM <em>table_name<\/em><\/p>\n\n\n\n<p>              ORDER BY <em>column_1 ASC|DESC,&#8230; column_n ASC|DESC<\/em><\/p>\n\n\n\n<p>ORDER BY column_1 ASC|DESC sorts the contents of column_1 in ascending order when ASC is specified, or descending order if DESC is specified.<\/p>\n\n\n\n<p>If the order key is not mentioned, ASC is chosen by default.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ORDER BY Multiple column (Major sort and Minor sort)<\/h2>\n\n\n\n<p>If more than one column is specified in ORDER BY, then the first column uses major sort and gets sorted on priority while the second column uses minor sort and gets sorted after the major sort.<\/p>\n\n\n\n<p>Note the order of the first column is maintained when the second column is sorted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ORDER BY in OVER()<\/h2>\n\n\n\n<p>Order by is used as a sub clause in over() function. It serves as a sorting function with the benefits of flexibility in querying columns in the SELECT statement.<\/p>\n\n\n\n<p>For example, I want to see how many employees are at Managers and AM positions against their sorted employee ID. The following statement will be used:&nbsp;<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\" style=\"margin-bottom:0px\"><strong>SELECT<\/strong> position_, employee_id, employee_name,&nbsp;<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\" style=\"margin-bottom:0px\"><strong>COUNT<\/strong>(employee_name) <strong>OVER<\/strong>(<strong>PARTITION<\/strong> <strong>BY<\/strong> position_ <strong>ORDER<\/strong> <strong>BY<\/strong> employee_id)<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\"><strong>FROM<\/strong> employee;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-7-1024x557.png\" alt=\"\" class=\"wp-image-2028\" width=\"512\" height=\"279\" srcset=\"https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-7-1024x557.png 1024w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-7-300x163.png 300w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-7-768x418.png 768w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-7-150x82.png 150w, https:\/\/testing.dicecamp.com\/insights\/wp-content\/uploads\/2022\/06\/img-7.png 1121w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n<\/div>\n\n\n<h1 class=\"wp-block-heading\">Take a Free Course! <\/h1>\n\n\n\n<p>It&#8217;s a fact that almost all database service providers across the data analytics industry use SQL as their default programming language. To know more about it you can refer to <a href=\"https:\/\/testing.dicecamp.com\/insights\/sql-and-databases-the-present-and-future\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQL and Database management<\/a> for an overview.<\/p>\n\n\n\n<p>Stating that, it creates a high demand for SQL as a basic skill in data analytics job roles. There are some renowned platforms that offers free training on SQL for free; we suggest <a href=\"https:\/\/www.codecademy.com\/learn\/learn-sql\" target=\"_blank\" rel=\"noreferrer noopener\">codecademy<\/a> and <a href=\"https:\/\/cognitiveclass.ai\/courses\/learn-sql-relational-databases\/\" target=\"_blank\" rel=\"noreferrer noopener\">coursera<\/a>.<\/p>\n\n\n\n<p><strong>PS <\/strong>once you get hands on in SQL, <a href=\"https:\/\/www.hackerrank.com\/domains\/sql\" target=\"_blank\" rel=\"noreferrer noopener\">Hackerank<\/a> is another amazing platform that could test your SQL ability through interesting and challenging problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn about advance SQL statements with examples; SQL JOINS, aggregate functions, GROUP BY, OVER(), PARTITION BY and ORDER BY<\/p>\n","protected":false},"author":7,"featured_media":1975,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":{"0":"post-1948","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-analytics"},"_links":{"self":[{"href":"https:\/\/testing.dicecamp.com\/insights\/wp-json\/wp\/v2\/posts\/1948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/testing.dicecamp.com\/insights\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/testing.dicecamp.com\/insights\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/testing.dicecamp.com\/insights\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/testing.dicecamp.com\/insights\/wp-json\/wp\/v2\/comments?post=1948"}],"version-history":[{"count":57,"href":"https:\/\/testing.dicecamp.com\/insights\/wp-json\/wp\/v2\/posts\/1948\/revisions"}],"predecessor-version":[{"id":2042,"href":"https:\/\/testing.dicecamp.com\/insights\/wp-json\/wp\/v2\/posts\/1948\/revisions\/2042"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/testing.dicecamp.com\/insights\/wp-json\/wp\/v2\/media\/1975"}],"wp:attachment":[{"href":"https:\/\/testing.dicecamp.com\/insights\/wp-json\/wp\/v2\/media?parent=1948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/testing.dicecamp.com\/insights\/wp-json\/wp\/v2\/categories?post=1948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/testing.dicecamp.com\/insights\/wp-json\/wp\/v2\/tags?post=1948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}