Data Science 100 Knocks (Structured Data Processing) – SQL part2 (Q21 to Q40)

Articles in English
Commentary :

The code is a SQL query that selects the count of rows from a table called "receipt".

The "SELECT" keyword is used to indicate that we want to retrieve data from the database.

The "COUNT" function is used to count the number of rows in the "receipt" table. In this case, the "COUNT(1)" statement is used to count the number of rows in the table where the value in the first column of each row is not null. This is equivalent to using "COUNT(*)", which counts all rows in the table regardless of whether any columns are null.

The "FROM" keyword is used to indicate the table we want to retrieve data from, in this case, the "receipt" table.

So, in summary, the query selects and returns the count of rows in the "receipt" table.
 
 
In [ ]:
 
 
 
 
Commentary :

This is a SQL query that retrieves data from a table called "receipt".

The query uses a Common Table Expression (CTE) called "product_cnt" to calculate the number of times each product has been sold at each store. It groups the data by "store_cd" and "product_cd" using the "GROUP BY" clause and counts the number of rows in each group using the "COUNT(1)" function. The results are stored in the "mode_cnt" column.

The query then uses another CTE called "product_mode" to rank the "mode_cnt" values for each store's products using the "RANK() OVER(PARTITION BY store_cd ORDER BY mode_cnt DESC)" function. This function assigns a rank to each product based on its "mode_cnt" value, within each store. The products with the highest mode count are assigned a rank of 1.

Finally, the query selects the top-selling product for each store using the "WHERE rnk = 1" clause to filter the results, and orders them by store and product code using the "ORDER BY" clause. It returns the store code, product code, and mode count for the top-selling product at each store, and limits the output to the first 10 rows using the "LIMIT" clause.

In summary, this query finds the top-selling product at each store, based on the number of times it has been sold, and returns the store code, product code, and mode count for each of the top 10 products.
 
 
 
 

 

 
 

 

Commentary :

This SQL code retrieves a count of the number of rows in a result set generated by performing a Cartesian product between the "store" and "product" tables.

The code starts with a SELECT statement that specifies the COUNT function and the value to be counted is "1", which means to count the number of rows.

The FROM clause specifies two tables to be used in the query - "store" and "product". However, instead of joining the two tables together based on some matching criteria, the CROSS JOIN keyword is used. This means that every row from the "store" table will be paired with every row from the "product" table, resulting in a Cartesian product.

The resulting output will be a single row with a single column containing the count of the number of rows in the Cartesian product. This count will be equal to the number of rows in the "store" table multiplied by the number of rows in the "product" table. This type of query is generally used to calculate the total number of possible combinations of data from two or more tables.
 
Data Science 100 Knocks (Structured Data Processing) - SQL
This is an ipynb file originally created by The Data Scientist Society(データサイエンティスト協会スキル定義委員) and translated from Japanese to English by DeepL. The reason I updated this file is to spread this practice, which is useful for everyone who wants to practice SQL, from beginners to advanced engineers. Since this data is created for Japanese, you may face language problems when practicing. But do not worry, it will not affect much.
Data Science 100 Knocks (Structured Data Processing) - SQL part1 (Q1 to Q20)
This is an ipynb file originally created by The Data Scientist Society(データサイエンティスト協会スキル定義委員) and translated from Japanese to English by DeepL. The reason I updated this file is to spread this practice, which is useful for everyone who wants to practice SQL, from beginners to advanced engineers. Since this data is created for Japanese, you may face language problems when practicing. But do not worry, it will not affect much.
Data Science 100 Knocks (Structured Data Processing) - SQL part2 (Q21 to Q40)
This is an ipynb file originally created by The Data Scientist Society(データサイエンティスト協会スキル定義委員) and translated from Japanese to English by DeepL. The reason I updated this file is to spread this practice, which is useful for everyone who wants to practice SQL, from beginners to advanced engineers. Since this data is created for Japanese, you may face language problems when practicing. But do not worry, it will not affect much.
Data Science 100 Knocks (Structured Data Processing) - SQL part3 (Q41 to Q60)
This is an ipynb file originally created by The Data Scientist Society(データサイエンティスト協会スキル定義委員) and translated from Japanese to English by DeepL. The reason I updated this file is to spread this practice, which is useful for everyone who wants to practice SQL, from beginners to advanced engineers. Since this data is created for Japanese, you may face language problems when practicing. But do not worry, it will not affect much.
Data Science 100 Knocks (Structured Data Processing) - SQL part4 (Q61 to Q80)
This is an ipynb file originally created by The Data Scientist Society(データサイエンティスト協会スキル定義委員) and translated from Japanese to English by DeepL. The reason I updated this file is to spread this practice, which is useful for everyone who wants to practice SQL, from beginners to advanced engineers. Since this data is created for Japanese, you may face language problems when practicing. But do not worry, it will not affect much.
Data Science 100 Knocks (Structured Data Processing) - SQL part5 (Q81 to Q100)
This is an ipynb file originally created by The Data Scientist Society(データサイエンティスト協会スキル定義委員) and translated from Japanese to English by DeepL. The reason I updated this file is to spread this practice, which is useful for everyone who wants to practice SQL, from beginners to advanced engineers. Since this data is created for Japanese, you may face language problems when practicing. But do not worry, it will not affect much.
Data Science 100 Knocks (Structured Data Processing)
This is an ipynb file originally created by The Data Scientist Society(データサイエンティスト協会スキル定義委員) and translated from Japanese to English by DeepL. The reason I updated this file is to spread this practice, which is useful for everyone who wants to practice Python, SQL, R, from beginners to advanced engineers. Since this data is created for Japanese, you may face language problems when practicing. But do not worry, it will not affect much.

Comment