At Johnson & Johnson, SQL is essential for analyzing pharmaceutical sales data, allowing them to track trends in medication usage and identify which products are performing best in different markets. It also helps them develop effective patient care models by analyzing medical records to understand patient needs and treatment outcomes, that is why Johnson & Johnson frequently asks SQL problems during interviews for Data Science, Data Engineering, and Data Analytics jobs.
So, to help you prepare, we've curated 9 Johnson & Johnson SQL interview questions – can you solve them?
As a data analyst for Johnson & Johnson, you need to track the average stars rating of each product on a monthly basis in order to gain insights on how well our products are received by consumers.
Given a database table that logs user reviews for different products as shown below:
review_id | user_id | submit_date | product_id | stars |
---|---|---|---|---|
6171 | 123 | 06/08/2022 00:00:00 | 50001 | 4 |
7802 | 265 | 06/10/2022 00:00:00 | 69852 | 4 |
5293 | 362 | 06/18/2022 00:00:00 | 50001 | 3 |
6352 | 192 | 07/26/2022 00:00:00 | 69852 | 3 |
4517 | 981 | 07/05/2022 00:00:00 | 69852 | 2 |
Can you write a SQL query to calculate the average stars rating for each product per month?
This query first extracts the month from using the built-in PostgreSQL function . It then calculates the average using the aggregate function and groups the data by the extracted month and . Finally, it orders the result by month and product ID. The resulting table should look something like this:
mth | product_id | avg_stars |
---|---|---|
6 | 50001 | 3.50 |
6 | 69852 | 4.00 |
7 | 69852 | 2.50 |
This output provides the average stars rating for each product on a monthly basis, which can be used to analyze product performance and user satisfaction over time.
p.s. Window functions show up super frequently during SQL interviews, so practice the 27+ window function questions on DataLemur
Discover Johnson & Johnson's focus on data science and digital health, revealing how they are leveraging research and technology to drive medical advancements! This knowledge can deepen your understanding of the connection between technology and healthcare innovation.
Given a table of Johnson & Johnson employee salaries, write a SQL query to find the 2nd highest salary among all employees.
employee_id | salary |
---|---|
1 | 2500 |
2 | 800 |
3 | 1000 |
4 | 1200 |
second_highest_salary |
---|
1200 |
You can solve this question directly within the browser on DataLemur:
You can find a step-by-step solution with hints here: 2nd Highest Salary.
The CHECK constraint is used to enforce rules on the data in a specific column. If a row is inserted or updated with data that does not follow the CHECK constraint's rule, the operation will fail. The CHECK constraint is often used with other constraints, such as NOT NULL or UNIQUE, to ensure that data meets certain conditions. You may want to use a CHECK constraint in your database if you want to maintain the quality and reliability of your data by ensuring that it meets specific criteria.
For example, you might use a CHECK constraint to ensure that a column contains only positive numbers, or that a date is within a certain range.
Johnson & Johnson is a multinational corporation involved in the production and sale of a broad range of healthcare products. The company has asked you to design a database to track the sales of its various products across different regions.
The primary components of this database are:
table that keeps track of each product's id, name, and price.
table that identifies different sales regions by their id and name.
table that records each individual sale, including the product id, region id, sale date and quantity sold.
Design the database in such a way that it enables the analysis of sales trends over time for different products and regions. Based on this database design, write a SQL query that returns the total sales revenue for each product per month.
product_id | product_name | price |
---|---|---|
101 | Product A | 100 |
102 | Product B | 150 |
103 | Product C | 200 |
region_id | region_name |
---|---|
1 | North |
2 | South |
3 | East |
4 | West |
sale_id | product_id | region_id | sale_date | quantity |
---|---|---|---|---|
1 | 101 | 1 | 2022-03-01 | 10 |
2 | 102 | 2 | 2022-03-02 | 15 |
3 | 102 | 3 | 2022-03-02 | 20 |
4 | 101 | 4 | 2022-04-01 | 15 |
5 | 102 | 4 | 2022-04-02 | 25 |
6 | 101 | 1 | 2022-05-01 | 12 |
7 | 103 | 2 | 2022-05-02 | 10 |
This query first gets the month from the sale_date field. It then joins the sales and products tables on the product_id field. The total_revenue is calculated by multiplying the quantity of each sale by the price of the product. The data is grouped by month and product_name, and ordered by month and total_revenue in descending order to show the products with the highest revenue first for each month.
Cross joins and natural joins are two types of JOIN operations in SQL that are used to combine data from multiple tables. A cross join creates a new table by combining each row from the first table with every row from the second table, and is also known as a cartesian join. On the other hand, a natural join combines rows from two or more tables based on their common columns, forming a new table. One key difference between these types of JOINs is that cross joins do not require common columns between the tables being joined, while natural joins do.
Here's an example of a cross join:
If you have 20 products and 10 colors, that's 200 rows right there!
Here's a natural join example using two tables, Johnson & Johnson employees and Johnson & Johnson managers:
This natural join returns all rows from Johnson & Johnson employees where there is no matching row in managers based on the column.
Given the following tables, and :
customer_id | first_name | last_name | country |
---|---|---|---|
1001 | John | Doe | United States |
1002 | Jane | Smith | United Kingdom |
1003 | Mary | Johnson | United States |
1004 | James | Brown | Canada |
1005 | Karen | Davis | Germany |
purchase_id | customer_id | product_id | purchase_date | product_category |
---|---|---|---|---|
2001 | 1001 | 3001 | 2022-03-01 | Healthcare |
2002 | 1002 | 3002 | 2022-03-05 | Personal Care |
2003 | 1001 | 3003 | 2022-04-01 | Healthcare |
2004 | 1004 | 3001 | 2022-06-01 | Healthcare |
2005 | 1005 | 3004 | 2022-06-03 | Personal Care |
Write a SQL query to find out all customers from the United States who have not made any purchases in the 'Personal Care' category.
This query first selects all customers from the United States and then uses a clause to eliminate any of these who have made a purchase in the 'Personal Care' category. The customers remaining after this filter have not made any purchases in this category.
A NULL value represents a missing or unknown value. It is different from a zero or a blank space, which are actual values. It is important to handle NULL values properly in SQL because they can cause unexpected results if not treated correctly.
As a part of our database administration team at Johnson & Johnson, you've been tasked with filtering our customer records. Specifically, using the SQL keyword LIKE, find those records where the customer's name starts with 'John'.
Given the following table representing the customer records,
customer_id | first_name | last_name | created_at | |
---|---|---|---|---|
145 | John | Doe | johndoe@example.com | 2021/06/05 00:00:00 |
657 | Johnson | Smith | johnsonsmith@example.com | 2021/07/15 00:00:00 |
839 | Emily | Swift | emilyswift@example.com | 2021/09/30 00:00:00 |
902 | Johnny | Cash | johnnycash@example.com | 2021/12/25 00:00:00 |
925 | Sam | Johnson | samjohnson@example.com | 2022/01/22 00:00:00 |
Find customers whose first name begins with 'John'.
The PostgreSQL query to solve this problem would be:
This will return all records from the customer table, where the starts with 'John'. It is important to note the '%' wildcard, which is used to match any sequence of characters.
customer_id | first_name | last_name | created_at | |
---|---|---|---|---|
145 | John | Doe | johndoe@example.com | 2021/06/05 00:00:00 |
657 | Johnson | Smith | johnsonsmith@example.com | 2021/07/15 00:00:00 |
902 | Johnny | Cash | johnnycash@example.com | 2021/12/25 00:00:00 |
Johnson & Johnson's sales department is looking to better understand their sales performance on a monthly basis and see how price changes may be affecting their sales. They want to calculate the average sales per month for each product, and the percent change in price for each product between consecutive months.
Tables:
sale_id | product_id | sale_date | units_sold | sale_price_per_unit |
---|---|---|---|---|
1 | 001 | 01/01/2022 | 100 | 10 |
2 | 002 | 01/01/2022 | 200 | 15 |
3 | 001 | 02/01/2022 | 150 | 12 |
4 | 002 | 02/01/2022 | 250 | 18 |
5 | 001 | 03/01/2022 | 80 | 11 |
product_id | product_name |
---|---|
001 | Product1 |
002 | Product2 |
This SQL query performs several operations. First, it calculates the monthly average unit sales and sale price per unit for each product in the CTE. Then, in the CTE, it calculates the percent price change from the previous month for each product. Finally, it joins these CTEs with the table to produce the final output, which provides the monthly average unit sales and percent price change for each product.
To practice a very similar question try this interactive Wayfair Y-on-Y Growth Rate Question which is similar for calculating growth rates using window functions or this Amazon Average Review Ratings Question which is similar for calculating average metrics on a month-to-month basis.
The best way to prepare for a Johnson & Johnson SQL interview is to practice, practice, practice. Besides solving the above Johnson & Johnson SQL interview questions, you should also solve the 200+ SQL coding questions which come from companies like Microsoft, Google, and Meta.
Each SQL question has multiple hints, fully explained answers along with a discussion board to see how others solved it and most importantly, there is an interactive coding environment so you can instantly run your query and have it checked.
To prep for the Johnson & Johnson SQL interview you can also be helpful to practice interview questions from other healthcare and pharmaceutical companies like:
However, if your SQL skills are weak, don't worry about going right into solving questions – refresh your SQL knowledge with this SQL tutorial for Data Analytics.
This tutorial covers SQL topics like LEAD window function and window functions like RANK() and ROW_NUMBER() – both of which come up often during SQL job interviews at Johnson & Johnson.
Beyond writing SQL queries, the other types of questions tested in the Johnson & Johnson Data Science Interview include:
To prepare for Johnson & Johnson Data Science interviews read the book Ace the Data Science Interview because it's got:
Also focus on the behavioral interview – prepare for it with this list of behavioral interview questions for Data Scientists.