At Kingsoft, SQL is typically used for analyzing user behavior data to improve gaming experiences. So, it shouldn't surprise you that Kingsoft almost always asks SQL coding questions in interviews for Data Analytics, Data Science, and Data Engineering jobs.
So, to help you study for the Kingsoft SQL interview, here’s 11 Kingsoft SQL interview questions – able to answer them all?
You are given a table named which, for every review given by a user to a product, stores the , , , and given by the user to that product.
Write a SQL query that shows the average stars given to each product within each calendar month. The output should be a table with month (), and .
Please use the PostgreSQL syntax.
review_id | user_id | submit_date | product_id | stars |
---|---|---|---|---|
1 | 123 | 01/15/2022 | 50001 | 4 |
2 | 265 | 01/16/2022 | 50001 | 3 |
3 | 362 | 02/10/2022 | 50001 | 5 |
4 | 192 | 02/25/2022 | 69852 | 4 |
5 | 981 | 02/28/2022 | 69852 | 2 |
This query uses the function to get the month from the field, and then groups the result by month and . It then calculates the average for each of the groups.
For more window function practice, solve this Uber SQL Interview Question on DataLemur's interactive coding environment:
Kingsoft, a software company, is trying to understand the purchasing habits of their customers better. They have customer data, product data and purchase records. As a data analyst, design the necessary tables and write a SQL query to determine the total value of purchase made by each customer last month.
For the purpose of this exercise, consider two tables - 'customers' and 'purchases'.
customer_id | full_name | signup_date |
---|---|---|
911 | John Doe | 2018-01-22 |
567 | Jane Smith | 2019-11-02 |
822 | Steve Johnson | 2019-07-14 |
454 | Lynda Davis | 2020-02-06 |
926 | Carol White | 2021-05-07 |
purchase_id | customer_id | product_id | purchase_date | purchase_price |
---|---|---|---|---|
781 | 911 | 103 | 2022-07-23 | 120.00 |
254 | 567 | 207 | 2022-07-30 | 85.00 |
632 | 822 | 308 | 2022-06-01 | 200.00 |
948 | 911 | 109 | 2022-07-31 | 110.00 |
753 | 926 | 205 | 2022-06-18 | 150.00 |
This query joins the 'customers' and 'purchases' tables based on the customer_id column and then filters out purchases made last month with the WHERE clause. Finally, the SUM aggregate function is used along with GROUP BY to calculate and return the total value of purchases for each customer last month.
Some similarities between unique and non-unique indexes include:
Some differences between unique and non-unique indexes include:
Kingsoft company wants to analyze their customer data to identify active users based on certain conditions. Active users are defined as those who have made at least one purchase each month over the last three months (Quarter). Using following customer activities data, write a SQL query to retrieve a list of active users.
user_id | purchase_date | product_id | product_category | purchase_amount |
---|---|---|---|---|
123 | 2022-05-06 | 1001 | Electronics | 200.50 |
123 | 2022-06-20 | 1010 | Electronics | 300.00 |
123 | 2022-07-15 | 2001 | Furniture | 1000.00 |
265 | 2022-05-06 | 2001 | Furniture | 1200.00 |
362 | 2022-05-15 | 3001 | Books | 50.00 |
362 | 2022-06-20 | 3100 | Books | 75.00 |
192 | 2022-07-26 | 1001 | Electronics | 250.00 |
user_id |
---|
123 |
362 |
This SQL query will give a list of all users who have made a purchase in each month for the last three months, thus providing a list of active users as defined by the company. The subquery counts the distinct months in which each user has made a purchase in the last three months. We then select the user IDs from this subquery where the count of distinct months is at least 3, indicating that they have been active for all three months.
Normalizing a database involves dividing a large table into smaller and more specialized ones, and establishing relationships between them using foreign keys. This reduces repetition, resulting in a database that is more adaptable, scalable, and easy to maintain. Additionally, it helps to preserve the accuracy of the data by reducing the likelihood of inconsistencies and problems.
As a data analyst at Kingsoft, a software and internet services company, you are tasked to analyze the sales data. Your CTO wants to know which product generates the highest average revenue each year. Using the and tables, write a SQL query to find the average revenue per product for each year.
sale_id | product_id | sale_date | quantity | price |
---|---|---|---|---|
1 | 1001 | 2020-01-01 | 5 | 50 |
2 | 1002 | 2021-03-10 | 2 | 100 |
3 | 1001 | 2020-05-15 | 3 | 50 |
4 | 1003 | 2021-02-02 | 1 | 300 |
5 | 1002 | 2021-10-06 | 4 | 100 |
product_id | product_name |
---|---|
1001 | Product A |
1002 | Product B |
1003 | Product C |
year | product_name | average_revenue |
---|---|---|
2020 | Product A | 54 |
2021 | Product B | 100 |
2021 | Product C | 300 |
This query first joins the and tables on the field to relate products to their related sales. It then groups by the year (extracted from the field with the extract function) and the . The function calculates the average revenue per product for each year. The clause then sorts the results first by year then by in descending order.
The / operator is used to remove to return all rows from the first SELECT statement that are not returned by the second SELECT statement.
Note that is available in PostgreSQL and SQL Server, while MINUS is available in MySQL and Oracle (but don't worry about knowing which DBMS supports which exact commands since Kingsoft interviewers aren't trying to trip you up on memorizing SQL syntax).
For a tangible example of in PostgreSQL, suppose you were doing an HR Analytics project for Kingsoft, and had access to Kingsoft's contractors and employees data. Assume that some employees were previously contractors, and vice versa, and thus would show up in both tables.
You could use operator to find all contractors who never were a employee using this query:
As a analyst at Kingsoft, you have been tasked to filter down customer records from the database for a specific email domain to support a marketing campaign. You have to find customers based on their email domain. For example, we might want to get all records of customers with a "kingsoft.com" email domain.
The table is structured as follows:
customer_id | customer_name | |
---|---|---|
001 | John Doe | johndoe@kingsoft.com |
002 | Jane Smith | janesmith@gmail.com |
003 | Sarah Johnson | sarahj@kingsoft.com |
004 | Michael Brown | brownmichael@yahoo.com |
005 | Emma Jones | emma.jones@kingsoft.com |
We want to output a table with the following structure:
customer_id | customer_name | |
---|---|---|
001 | John Doe | johndoe@kingsoft.com |
003 | Sarah Johnson | sarahj@kingsoft.com |
005 | Emma Jones | emma.jones@kingsoft.com |
Based on the requirement, below is the PostgreSQL query to get the specific customer records:
This query selects customer_id, customer_name and email from the customers table where the email address includes "@kingsoft.com". The '%' sign is used to define wildcards (missing letters) both before and after the '@kingsoft.com'.
Kingsoft, a leading software company, maintains a database of users, product purchases, and product reviews. The team at Kingsoft wants to compare the average ratings of their products in different months to analyze customer feedback and improve their product quality.
You are tasked with writing a SQL query to fetch the average ratings of all the products purchased in different months. The average rating should be up to two decimal places. The rating rankings should be grouped first by month (), and then by the product's ID ().
You have been given two tables: and .
The table is structured as follows:
purchase_id | customer_id | purchase_date | product_id |
---|---|---|---|
1001 | 123 | 01/09/2022 | 50001 |
1002 | 456 | 02/09/2022 | 69852 |
1003 | 789 | 03/10/2022 | 50001 |
1004 | 123 | 01/11/2022 | 69852 |
1005 | 789 | 03/12/2022 | 69852 |
The table is structured as follows:
review_id | user_id | submit_date | product_id | stars |
---|---|---|---|---|
6171 | 123 | 06/08/2022 | 50001 | 4 |
7802 | 456 | 06/10/2022 | 69852 | 4 |
5293 | 789 | 06/18/2022 | 50001 | 3 |
6352 | 123 | 07/26/2022 | 69852 | 3 |
4517 | 789 | 07/05/2022 | 69852 | 2 |
This PostgreSQL query joins the and tables based on the customer's ID and the product's ID, then calculates the average after rounding to two decimal places. The result is grouped by month and product_id.The order is first by month and then by product_id.
This will provide the average ratings for each product for each month, which the Kingsoft team can use to understand customer feedback and improve their products.
Since join questions come up frequently during SQL interviews, take a stab at an interactive Spotify JOIN SQL question:
"In SQL, a join generally retrieves rows from multiple tables and combines them into a single result set. For an example of the difference between a left vs. right join, suppose you had a table of Kingsoft orders and Kingsoft customers.
A retrieves all rows from the left table (in this case, the Orders table) and any matching rows from the right table (the Customers table). If there is no match in the right table, NULL values will be returned for the right table's columns.
A combines all rows from the right table (in this case, the Customers table) and any matching rows from the left table (the Orders table). If there is no match in the left table, NULL values will be displayed for the left table's columns.
Kingsoft, a software company, has two main databases -- one for its products () and one for its sales (). The table contains details about the cost and selling price of the products, while the table contains details about the quantity sold.
The task is to write a SQL query to calculate the profit margin for each product. The profit margin is calculated as the difference between the selling price and the cost price expressed as a percentage of the selling price. Use the ROUND() function to round the profit margin to two decimal places.
product_id | product_name | cost_price | selling_price |
---|---|---|---|
1 | Product A | 10 | 15 |
2 | Product B | 20 | 25 |
3 | Product C | 30 | 35 |
sale_id | product_id | quantity_sold |
---|---|---|
101 | 1 | 500 |
102 | 2 | 300 |
103 | 3 | 700 |
This query first calculates the profit for each sale by subtracting the cost price from the selling price. It then expresses this profit as a percentage of the selling price. The ROUND() function in PostgreSQL is used to round the resulting profit margin to two decimal places. The JOIN keyword is used to combine rows from the 'products' and 'sales' tables based on the common product_id column present in both tables.
The key to acing a Kingsoft SQL interview is to practice, practice, and then practice some more! Beyond just solving the above Kingsoft SQL interview questions, you should also solve the 200+ SQL questions from real Data Science & Analytics interviews which come from companies like Google, Microsoft and Silicon Valley startups.
Each interview question has multiple hints, fully explained answers along with a discussion board to see how others solved it and best of all, there is an interactive coding environment so you can easily right in the browser your SQL query answer and have it checked.
To prep for the Kingsoft SQL interview you can also be helpful to practice interview questions from other tech companies like:
However, if your SQL foundations are weak, forget about diving straight into solving questions – strengthen your SQL foundations with this free SQL tutorial.
This tutorial covers SQL concepts such as RANK() window functions and aggreage functions like MIN()/MAX() – both of these show up frequently during SQL job interviews at Kingsoft.
In addition to SQL interview questions, the other question categories to prepare for the Kingsoft Data Science Interview are:
To prepare for Kingsoft Data Science interviews read the book Ace the Data Science Interview because it's got: