9 Baidu SQL Interview Questions (Updated 2024)

Updated on

February 14, 2024

At Baidu, SQL is used day-to-day for analyzing user behavior patterns to fine-tune search algorithms, and managing large datasets to power the Baidu advertisement unit. Unsurprisingly this is why Baidu LOVES to ask SQL query questions in interviews for Data Analytics, Data Science, and Data Engineering jobs.

So, to help you practice for the Baidu SQL interview, this blog covers 9 Baidu SQL interview questions – can you solve them?

9 Baidu SQL Interview Questions

SQL Question 1: Find the Top Active Users on Baidu

Baidu, being a web services company, greatly values its users. Power users, or those who frequently engage in the company's various activities, are of particular importance. For this assessment, assume that a "power user" is defined as a user who performs a significant number of searches monthly.

You are provided the following table with the following columns:

  • : a unique identifier for each search
  • : the ID for the user who performed the search
  • : the date the search was performed with the format 'YYYY-MM-DD'

Please write a SQL query to find the top 5 users who performed the highest number of searches in the past month and provide both their and the number of searches they performed.

Example - Input:


(Note: This is a simplified table and does not include other potential columns such as or , which are irrelevant for this question)


This query calculates the number of searches each user performed in the past month by counting the number of unique 's each performed. This result is then ordered in descending order and limited to the 5 users with the most searches. As such, the output will be a list of the top 5 users and the number of searches they performed in the last month.

To solve a related customer analytics question on DataLemur's free online SQL coding environment, try this Microsoft Azure Cloud SQL Interview Question: Microsoft SQL Interview Question: Super Cloud Customer

SQL Question 2: Analysis of Users' Search History

As a data analyst at Baidu, you have been tasked with performing analysis on users' search history data. You have been asked to find out the most popular search terms per user for every month.

The database has a table with following structure.

Example Input:
10112304/02/2022machine learning
10312304/04/2022machine learning
10423404/05/2022big data
10612304/07/2022machine learning
10712305/02/2022data science
10912305/04/2022data science

In the table, is a unique identifier of the search, is the identifier of the search's user, is the date when the search was performed, and is the term that was searched.

Write a SQL query to find out the most popular search terms per user for each month. If there are multiple search terms with the same frequency, choose the most recent one.

Example Output:
4123machine learning
5123data science


The following PostgreSQL query can solve this problem:

This query calculates the frequency of each search term per user per month with a window function in the CTE . Then it selects the most popular (most frequent) search term per user per month. If there are multiple search terms with the same frequency, the most recent one is selected due to in the clause.

For more window function practice, try this Uber SQL problem on DataLemur's online SQL coding environment:

Uber Data Science SQL Interview Question

SQL Question 3: What's the difference between a foreign and primary key?

To explain the difference between a primary key and foreign key, let's start with some data from Baidu's marketing analytics database which stores data from Google Ads campaigns:


+------------+------------+------------+------------+ | ad_id | campaign_id| keyword | click_count| +------------+------------+------------+------------+ | 1 | 100 | Baidu pricing | 10 | | 2 | 100 | Baidu reviews | 15 | | 3 | 101 | Baidu alternatives | 7 | | 4 | 101 | buy Baidu | 12 | +------------+------------+------------+------------+

In this table, could be a primary key. It is unique for each row in the table and cannot contain null values.

could be a foreign key. It references the of the Google Ads campaign that each ad belongs to, establishing a relationship between the ads and their campaigns. This foreign key allows you to easily query the table to find out which ads belong to a specific campaign, or to find out which campaigns a specific ad belongs to.

It is also possible for a table to have multiple foreign keys that reference different primary keys in other tables. For example, the table could have additional foreign keys for the of the ad group that each ad belongs to, and the of the Google Ads account that the campaigns belong to.

Baidu SQL Interview Questions

SQL Question 4: User Information and Search Queries Analysis

As a data analyst at Baidu, one of your tasks is to understand the search behavior of different groups of users. The system has gathered user information, including their registration dates and regions. Additionally, the system logs all search queries made by these users along with timestamp. The aim is to find out the number of unique users in every region who have made at least one search query each month.

Baidu's user information can be stored in a table and the search activity in a table:

Example Input:
45212019-11-27East Asia
38572020-06-12South Asia
41892021-05-22North America
12342020-02-18South America
Example Input:
38572022-07-01Big Data
38572022-07-05Machine Learning
29532022-06-30Artificial Intelligence
12342022-07-11Data Science


You can use the following SQL query in PostgreSQL to solve the problem:

This query joins and tables on , then groups by each month and region. Within each group, it counts the number of distinct to determine the monthly active users per region. The function truncates the search date to month, and the casting is there to convert the timestamp (with time and possibly timezone) to a simple date format.

SQL Question 5: What's the SQL command do, and can you give an example?

combines the results from two or more SELECT statements, but only returns the rows that are exactly the same between the two sets.

For example, let's use to find all of Baidu's Facebook video ads with more than 10k views that are also being run on YouTube:

SQL Question 6: Average Number of Clicked Ads Per Day in Baidu

Baidu is a Chinese multinational technology company specializing in Internet-related services and products, and artificial intelligence. One valuable metric for them might be the average number of clicked ads per day. The following tables represent simplified versions of Baidu's , , and databases:

Example Input:
12301/08/2022South China
26511/12/2021North China
36225/07/2022East China
19210/08/2021West China
98115/01/2022Central China
Example Input:
Example Input:

Using these tables, write a SQL query to find the average number of ads clicked per day for all users.

Example Output:


The provided SQL query first groups the clicks per day, calculating the number of clicks each day. It then calculates the average number of clicks per day by using a window function that orders the data by click date. Finally, it orders the resulting output by click date.

To practice a very similar question try this interactive Facebook App Click-through Rate (CTR) Question which is similar for calculating click rates or this Facebook Active User Retention Question which is similar for handling user data.

SQL Question 7: What's a database view, and when would you use one?

Database views are virtual tables based on the results of a SQL statement. They're just like vanilla tables, except views allow you to create simplified versions of tables or hide sensitive data from certain users.

In PostgreSQL, you can create a view by using the command. Here's an example for the table:

SQL Question 8: Average Search Query Duration

You are a data analyst at Baidu, a popular search engine company in China. The development team needs to check if some search categories are slowing down. Your task is to find the average duration of search queries for each search category over the last month.

Please consider the following "searches" table for this analysis:

Example Input:
10145608/10/2022 10:15:00Tech0.15
10278908/12/2022 11:25:00Fashion0.18
10345608/20/2022 13:33:00Travel0.2
10432108/21/2022 16:00:00Tech0.12
10512308/23/2022 17:45:00Fashion0.16

Your task is to provide a report that provides the average search duration by category for the last month. The output should include the month (in YYYY-MM format), search category, and average search duration up to two decimal places.

Example Output:


This SQL query rounds the average search duration to two decimal places for each search category. It runs for the last month, considering the current date as the end point. For instance, if the current date is September 15, then it will consider the period from August 1 to August 31. The results are grouped by month and search category.

SQL Question 9: Baidu Web Traffic Analysis

As a data analyst at Baidu, you've been given a dataset of user website visit logs. The user visits are marked with a timestamp. Your task is to generate a report that shows the number of daily unique visitors for the past week.

The table has the following structure:

Example Input:
11232022-07-29 08:30:00
21232022-07-29 10:20:00
32652022-07-28 15:42:00
43622022-07-28 22:12:00
51922022-07-27 16:45:00
61922022-07-27 18:55:00
79812022-07-26 13:22:00
89812022-07-31 08:44:00
95252022-07-25 05:40:00
105252022-07-25 06:10:00
112642022-07-24 14:42:00
121232022-07-24 15:22:00

We would like to produce the following result:

Example Output:


A PostgreSQL query that can be used to answer this question is:

This query groups the logs by the date of visit (ignoring the time part) for the past 7 days. It counts the number of unique user IDs for each group, which gives us the number of unique visitors each day. The result is then ordered by the visit date in ascending order.

Preparing For The Baidu SQL Interview

The best way to prepare for a Baidu SQL interview is to practice, practice, practice. In addition to solving the above Baidu SQL interview questions, you should also solve the 200+ tricky sql questions on DataLemur which come from companies like FAANG and tech startups. DataLemur SQL and Data Science Interview Questions

Each DataLemur SQL question has multiple hints, step-by-step solutions and most importantly, there is an interactive coding environment so you can right online code up your SQL query and have it graded.

To prep for the Baidu SQL interview it is also wise to solve SQL problems from other tech companies like:

In case your SQL coding skills are weak, don't worry about going right into solving questions – go learn SQL with this SQL tutorial for Data Scientists & Analysts.

SQL interview tutorial

This tutorial covers topics including math functions like ROUND()/CEIL() and AND/OR/NOT – both of these pop up often in SQL interviews at Baidu.

Baidu Data Science Interview Tips

What Do Baidu Data Science Interviews Cover?

Besides SQL interview questions, the other question categories covered in the Baidu Data Science Interview are:

Baidu Data Scientist

How To Prepare for Baidu Data Science Interviews?

To prepare for Baidu Data Science interviews read the book Ace the Data Science Interview because it's got:

  • 201 interview questions sourced from Microsoft, Amazon & startups
  • a refresher covering Product Analytics, SQL & ML
  • over 900+ 5-star reviews on Amazon

Ace the Data Science Interview by Nick Singh Kevin Huo