sql Flashcards
learn sql
Given the table Products:
\+ -------------+---------+ \+-------------+---------+ | product_id | int | | low_fats | enum | | recyclable | enum | \+-------------+---------+
Write a solution to find the ids of products that are both low fat and recyclable.
Return the result table in any order.
Column Name | Type |
SELECT product_id from products where low_fats = 'Y' and recyclable = 'Y';
Table: Customer \+-------------+---------+ \+-------------+---------+ | id | int | | name | varchar | | referee_id | int | \+-------------+---------+
In SQL, id is the primary key column for this table.
Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.
Find the names of the customer that are not referred by the customer with id = 2.
Column Name | Type |
SELECT name FROM customer WHERE referee_id <> 2 OR referee_id IS NULL;
A country is big if:
it has an area of at least three million (i.e., 3000000 km2), or
it has a population of at least twenty-five million (i.e., 25000000).
Write a solution to find the name, population, and area of the big countries.
Return the result table in any order.
The result format is in the following example.
SELECT name, population, area from world where area >= 3000000 or population >= 25000000;
Write a solution to find all the authors that viewed at least one of their own articles.
select distinct(author_id) as id from Views where author_id = viewer_id order by author_id asc;
Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15.
SELECT tweet_id FROM Tweets
WHERE LENGTH(content) > 15;
Write a solution to show the unique ID of each user, If a user does not have a unique ID replace just show null.
Return the result table in any order.
SELECT eu.unique_id, e.name from Employees e left join EmployeeUNI eu on e.id = eu.id;
Table: Sales \+-------------+-------+ \+-------------+-------+ | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | \+-------------+-------+ Table: Product \+--------------+---------+ | Column Name | Type | \+--------------+---------+ | product_id | int | | product_name | varchar | \+--------------+---------+
Write a solution to report the product_name, year, and price for each sale_id in the Sales table.
Return the resulting table in any order.
Column Name | Type |
select p.product_name, s.year, s.price from sales s join product p on s.product_id = p.product_id;
Table: Visits \+-------------+---------+ \+-------------+---------+ | visit_id | int | | customer_id | int | \+-------------+---------+ Table: Transactions \+----------------+---------+ | Column Name | Type | \+----------------+---------+ | transaction_id | int | | visit_id | int | | amount | int | \+----------------+---------+
Write a solution to find the IDs of the users who visited without making any transactions and the number of times they made these types of visits.
Column Name | Type |
select v.customer_id, count(v.visit_id) as count_no_trans from Visits v Left join Transactions t on v.visit_id = t.visit_id where t.transaction_id is null group by v.customer_id
Table: Weather \+---------------+---------+ \+---------------+---------+ | id | int | | recordDate | date | | temperature | int | \+---------------+---------+
Write a solution to find all dates’ id with higher temperatures compared to its previous dates (yesterday).
Column Name | Type |
~~~
```SELECT today.id
FROM Weather yesterday
CROSS JOIN Weather today
WHERE DATEDIFF(today.recordDate,yesterday.recordDate) = 1
AND today.temperature > yesterday.temperature
Table: Activity \+----------------+---------+ \+----------------+---------+ | machine_id | int | | process_id | int | | activity_type | enum | | timestamp | float | \+----------------+---------+
There is a factory website that has several machines each running the same number of processes. Write a solution to find the average time each machine takes to complete a process.
Column Name | Type |
select a.machine_id, round( (select avg(a1.timestamp) from Activity a1 where a1.activity_type = 'end' and a1.machine_id = a.machine_id) - (select avg(a1.timestamp) from Activity a1 where a1.activity_type = 'start' and a1.machine_id = a.machine_id) ,3) as processing_time from Activity a group by a.machine_id
Table: Employee \+-------------+---------+ \+-------------+---------+ | empId | int | | name | varchar | | supervisor | int | | salary | int | \+-------------+---------+ Table: Bonus \+-------------+------+ | Column Name | Type | \+-------------+------+ | empId | int | | bonus | int | \+-------------+------+
Write a solution to report the name and bonus amount of each employee with a bonus less than 1000.
Column Name | Type |
SELECT Employee.name,Bonus.bonus FROM Employee LEFT JOIN Bonus ON Employee.empID = Bonus.empID WHERE bonus < 1000 OR Bonus IS NULL ;
Table: Students \+---------------+---------+ \+---------------+---------+ | student_id | int | | student_name | varchar | \+---------------+---------+ Table: Subjects \+--------------+---------+ | Column Name | Type | \+--------------+---------+ | subject_name | varchar | \+--------------+---------+ Table: Examinations \+--------------+---------+ | Column Name | Type | \+--------------+---------+ | student_id | int | | subject_name | varchar | \+--------------+---------+
Write a solution to find the number of times each student attended each exam.
Return the result table ordered by student_id and subject_name.
| Column Name | Type |
Column Name | Type |
SELECT S.student_id ,S.student_name ,Su.subject_name ,COUNT(E.student_id) attended_exams FROM Students S CROSS JOIN Subjects Su LEFT JOIN Examinations E ON S.student_id = E.student_id AND Su.subject_name = E.subject_name GROUP BY S.student_id, S.student_name, Su.subject_name ORDER BY S.student_id, S.student_name, Su.subject_name
Table: Employee \+-------------+---------+ \+-------------+---------+ | name | varchar | | department | varchar | | managerId | int | \+-------------+---------+
Write a solution to find managers with at least five direct reports.
| id | int |
Column Name | Type |
SELECT a.name FROM Employee a JOIN Employee b ON a.id = b.managerId GROUP BY b.managerId HAVING COUNT(*) >= 5
Table: Signups \+----------------+----------+ \+----------------+----------+ | user_id | int | | time_stamp | datetime | \+----------------+----------+ Table: Confirmations \+----------------+----------+ | Column Name | Type | \+----------------+----------+ | user_id | int | | time_stamp | datetime | | action | ENUM | \+----------------+----------+
The confirmation rate of a user is the number of ‘confirmed’ messages divided by the total number of requested confirmation messages. The confirmation rate of a user that did not request any confirmation messages is 0. Round the confirmation rate to two decimal places.
Write a solution to find the confirmation rate of each user.
Column Name | Type |
select s.user_id, round( avg( if( c.action="confirmed",1,0 ) ),2) as confirmation_rate from Signups as s left join Confirmations as c on s.user_id= c.user_id group by user_id;
Table: Cinema \+----------------+----------+ \+----------------+----------+ | id | int | | movie | varchar | | description | varchar | | rating | float | \+----------------+----------+
Column Name | Type |
Select * from Cinema
where MOD(id, 2) != 0 and description != ‘boring’
order by rating desc;
Table: Prices
\+---------------+---------+ | Column Name | Type | \+---------------+---------+ | product_id | int | | start_date | date | | end_date | date | | price | int | \+---------------+---------+
Table: UnitsSold
\+---------------+---------+ | Column Name | Type | \+---------------+---------+ | product_id | int | | purchase_date | date | | units | int | \+---------------+---------+
Write a solution to find the average selling price for each product. average_price should be rounded to 2 decimal places. If a product does not have any sold units, its average selling price is assumed to be 0.
SELECT p.product_id, IFNULL(ROUND(SUM(u.units * p.price)/SUM(u.units), 2),0) as average_price FROM Prices p LEFT JOIN UnitsSold u ON p.product_id = u.product_id AND u.purchase_date BETWEEN p.start_date and p.end_date GROUP BY p.product_id
Table: Project \+-------------+---------+ | Column Name | Type | \+-------------+---------+ | project_id | int | | employee_id | int | \+-------------+---------+ Table: Employee \+------------------+---------+ | Column Name | Type | \+------------------+---------+ | employee_id | int | | name | varchar | | experience_years | int | \+------------------+---------+
Write an SQL query that reports the average experience years of all the employees for each project, rounded to 2 digits.
SELECT p.project_id, ROUND(AVG(e.experience_years), 2) as average_years FROM Project p JOIN Employee e ON p.employee_id = e.employee_id GROUP BY p.project_id
Table: Users \+-------------+---------+ | Column Name | Type | \+-------------+---------+ | user_id | int | | user_name | varchar | \+-------------+---------+ Table: Register \+-------------+---------+ | Column Name | Type | \+-------------+---------+ | contest_id | int | | user_id | int | \+-------------+---------+
Write a solution to find the percentage of the users registered in each contest rounded to two decimals.
select contest_id, round(count(distinct user_id)* 100/(select count(user_id) from Users), 2) as percentage from Register group by contest_id order by percentage desc, contest_id
Table: Queries \+-------------+---------+ | Column Name | Type | \+-------------+---------+ | query_name | varchar | | result | varchar | | position | int | | rating | int | \+-------------+---------+
We define query quality as:
The average of the ratio between query rating and its position.
We also define poor query percentage as:
The percentage of all queries with rating less than 3.
Write a solution to find each query_name, the quality and poor_query_percentage.
SELECT query_name, ROUND(AVG(rating/position), 2) AS quality, ROUND(SUM(IF(rating < 3, 1, 0)) / COUNT(*) * 100, 2) AS poor_query_percentage FROM Queries where query_name is not null GROUP BY query_name;
Table: Transactions \+---------------+---------+ | Column Name | Type | \+---------------+---------+ | id | int | | country | varchar | | state | enum | | amount | int | | trans_date | date | \+---------------+---------+
Write an SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.
select DATE_FORMAT(trans_date, '%Y-%m') AS month, country, count(id) as trans_count, sum(CASE WHEN state = 'approved' THEN 1 ELSE 0 END) as approved_count, sum(amount) as trans_total_amount, sum(case when state = 'approved' then amount else 0 end) as approved_total_amount FROM Transactions group by month, country;