This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Leetcode submissions

1 - 2025-08-10 10:14:07 +0000 UTC

Daily Leads and Partners

Code

SELECT 
    DailySales.date_id,
    DailySales.make_name,
    COUNT(DISTINCT DailySales.lead_id) AS unique_leads,
    COUNT(DISTINCT DailySales.partner_id) AS unique_partners
FROM 
    DailySales
GROUP BY 
    DailySales.date_id,
    DailySales.make_name;

2 - 2025-08-10 10:10:37 +0000 UTC

Invalid Tweets

Code

SELECT
    Tweets.tweet_id
FROM
    Tweets
WHERE
    LENGTH(Tweets.content) > 15

3 - 2025-08-10 09:56:18 +0000 UTC

Fix Names in a Table

Code

SELECT 
    Users.user_id,
    UPPER(LEFT(Users.name, 1)) 
        || LOWER(SUBSTRING(Users.name FROM 2)) AS name
FROM 
    Users
ORDER BY 
    Users.user_id;

4 - 2025-08-10 09:52:07 +0000 UTC

Average Time of Process per Machine

Code

SELECT 
    act1.machine_id, 
    ROUND(AVG(act2.timestamp - act1.timestamp)::decimal, 3) AS processing_time
FROM
    Activity act1,
    Activity act2
WHERE 
    act1.machine_id = act2.machine_id
    AND act1.process_id = act2.process_id
    AND act1.activity_type = 'start'
    AND act2.activity_type = 'end'
GROUP BY 
    act1.machine_id;

5 - 2025-08-10 08:14:30 +0000 UTC

Percentage of Users Attended a Contest

Code

WITH total_users AS (
    SELECT 
        COUNT(DISTINCT user_id) AS total_count
    FROM 
        Users
)
SELECT 
    Register.contest_id,
    ROUND(
        COUNT(DISTINCT Register.user_id) * 100.0 / total_users.total_count,
        2
    ) AS percentage
FROM
    Register
CROSS JOIN 
    total_users
GROUP BY 
    Register.contest_id, 
    total_users.total_count
ORDER BY 
    percentage DESC, 
    Register.contest_id;

6 - 2025-08-07 14:57:34 +0000 UTC

Bank Account Summary II

Code

SELECT
    Users.name AS "NAME",
    SUM(Transactions.amount) AS "BALANCE"
FROM
    Users
LEFT JOIN
    Transactions
ON
    Users.account = Transactions.account
GROUP BY
    Users.account, Users.name
HAVING
    SUM(Transactions.amount) > 10000

7 - 2025-08-06 16:26:47 +0000 UTC

Customer Who Visited but Did Not Make Any Transactions

Code

SELECT
    Visits.customer_id,
    COUNT(Visits.visit_id) AS "count_no_trans"
FROM
    Visits
LEFT JOIN
    Transactions
ON
    Visits.visit_id = Transactions.visit_id
WHERE
    Transactions.visit_id IS NULL
GROUP BY
    Visits.customer_id

8 - 2025-08-03 11:36:46 +0000 UTC

Patients With a Condition

Code

SELECT
    Patients.patient_id,
    Patients.patient_name,
    Patients.conditions
FROM
    Patients
WHERE
    Patients.conditions ~ '(^|\s)DIAB1'

9 - 2025-08-03 11:29:35 +0000 UTC

Find Users With Valid E-Mails

Code

SELECT 
    *
FROM 
    Users
WHERE 
    Users.mail ~ '^[a-zA-Z][a-zA-Z0-9\.\-_]*@leetcode\.com$';

10 - 2025-08-03 11:14:27 +0000 UTC

Group Sold Products By The Date

Code

SELECT 
    Activities.sell_date, 
    COUNT(
        DISTINCT (Activities.product, Activities.sell_date)
    ) AS num_sold, 
    STRING_AGG(
        DISTINCT Activities.product, ',' ORDER BY product
    ) AS products
FROM 
    Activities
GROUP BY 
    Activities.sell_date
ORDER BY 
    Activities.sell_date;

11 - 2025-08-02 17:09:46 +0000 UTC

Top Travellers

Code

SELECT
    Users.name, 
    COALESCE(SUM(Rides.distance), 0) AS "travelled_distance"
FROM
    Users
LEFT JOIN
    Rides
ON
    Users.id = Rides.user_id
GROUP BY
    Users.id, Users.name
ORDER BY
    travelled_distance DESC,
    Users.name ASC

12 - 2025-08-02 16:28:39 +0000 UTC

Replace Employee ID With The Unique Identifier

Code

SELECT
    EmployeeUNI.unique_id, Employees.name
FROM
    Employees
LEFT JOIN
    EmployeeUNI
ON
    Employees.id = EmployeeUNI.id

13 - 2025-08-02 15:33:48 +0000 UTC

List the Products Ordered in a Period

Code

SELECT 
    Products.product_name, SUM(Orders.unit) as "unit"
FROM 
    Products
JOIN 
    Orders
ON 
    Orders.product_id = Products.product_id
WHERE 
    Orders.order_date BETWEEN '2020-02-01' AND '2020-02-29'
GROUP BY 
    Products.product_name
HAVING 
    SUM(Orders.unit) >= 100;

14 - 2025-08-02 14:24:46 +0000 UTC

Students and Examinations

Code

SELECT
    Students.student_id, 
    Students.student_name,
    Subjects.subject_name,
    COUNT(Examinations.subject_name) as "attended_exams"
FROM
    Students
CROSS JOIN
    Subjects
LEFT JOIN
    Examinations
ON
    Students.student_id = Examinations.student_id
    AND Subjects.subject_name = Examinations.subject_name
GROUP BY
    Students.student_id, 
    Students.student_name, 
    Subjects.subject_name
ORDER BY
    Students.student_id, 
    Subjects.subject_name

15 - 2025-08-02 13:25:49 +0000 UTC

Average Selling Price

Code

SELECT 
    Prices.product_id,
    CASE
        WHEN 
            SUM(UnitsSold.units) is null
        THEN 
            0
        ELSE
            ROUND(SUM(UnitsSold.units * Prices.price) / SUM(UnitsSold.units)::numeric, 2)
    END AS average_price
FROM 
    Prices
LEFT JOIN 
    UnitsSold
ON 
    UnitsSold.product_id = Prices.product_id 
    AND UnitsSold.purchase_date 
        BETWEEN Prices.start_date AND Prices.end_date
GROUP BY 
    Prices.product_id;

16 - 2025-08-02 12:46:47 +0000 UTC

Queries Quality and Percentage

Code

SELECT 
    query_name,  
    ROUND(SUM(rating * 1.0 / position) / COUNT(*), 2) AS quality,
    ROUND(
        COUNT(CASE WHEN rating < 3 THEN 1 END) * 1.0 / COUNT(*), 
        4
    ) * 100 AS poor_query_percentage
FROM 
    Queries
GROUP BY 
    query_name;

17 - 2025-08-02 12:01:25 +0000 UTC

Reformat Department Table

Code

SELECT id,
        SUM(
            CASE 
                WHEN 
                    month = 'Jan' 
                THEN 
                    revenue 
                ELSE 
                    NULL 
            END
        ) AS Jan_Revenue,
        SUM(
            CASE 
                WHEN 
                    month = 'Feb' 
                THEN 
                    revenue 
                ELSE 
                    NULL 
                END
        ) AS Feb_Revenue,
        SUM(
            CASE 
                WHEN 
                    month = 'Mar' 
                THEN 
                    revenue 
                ELSE 
                    NULL 
                END
        ) AS Mar_Revenue,
        SUM(
            CASE 
                WHEN 
                    month = 'Apr' 
                THEN 
                    revenue 
                ELSE 
                    NULL 
                END
        ) AS Apr_Revenue,
        SUM(
            CASE 
                WHEN 
                    month = 'May' 
                THEN 
                    revenue 
                ELSE 
                    NULL 
                END
        ) AS May_Revenue,
        SUM(
            CASE 
                WHEN 
                    month = 'Jun' 
                THEN 
                    revenue 
                ELSE 
                    NULL 
                END
        ) AS Jun_Revenue,
        SUM(
            CASE 
                WHEN 
                    month = 'Jul'
                THEN 
                    revenue 
                ELSE 
                    NULL 
                END
        ) AS Jul_Revenue,
        SUM(
            CASE 
                WHEN 
                    month = 'Aug'
                THEN 
                    revenue 
                ELSE 
                    NULL
                END
        ) AS Aug_Revenue,
        SUM(
            CASE 
                WHEN 
                    month = 'Sep' 
                THEN 
                    revenue 
                ELSE 
                    NULL 
                END
        ) AS Sep_Revenue,
        SUM(
            CASE 
                WHEN 
                    month = 'Oct' 
                THEN 
                    revenue 
                ELSE 
                    NULL 
            END
        ) AS Oct_Revenue,
        SUM(
            CASE 
                WHEN 
                    month = 'Nov' 
                THEN 
                    revenue 
                ELSE 
                    NULL 
            END
        ) AS Nov_Revenue,
        SUM(
            CASE 
                WHEN 
                    month = 'Dec' 
                THEN 
                    revenue 
                ELSE 
                    NULL 
            END
        ) AS Dec_Revenue
FROM 
    Department
GROUP BY 
    id;

18 - 2025-08-02 11:21:50 +0000 UTC

Article Views I

Code

SELECT DISTINCT
    views1.author_id as "id"
FROM
    Views views1
CROSS JOIN
    Views views2
WHERE
    views1.author_id = views2.viewer_id
    AND views1.author_id = views2.author_id
ORDER BY
    views1.author_id ASC

19 - 2025-08-02 11:15:20 +0000 UTC

User Activity for the Past 30 Days I

Code

SELECT 
    activity_date AS day, 
    COUNT(DISTINCT(user_id)) AS active_users  
FROM 
    Activity 
WHERE 
    activity_date 
        BETWEEN '2019-07-27'::DATE - INTERVAL '29 DAYS' 
        AND '2019-07-27'::DATE
GROUP BY 
    activity_date

20 - 2025-08-02 09:22:49 +0000 UTC

Sales Analysis III

Code

SELECT 
    Product.product_id, Product.product_name
FROM 
    Sales
JOIN 
    Product 
ON 
    Product.product_id = Sales.product_id
GROUP BY 
    Product.product_id, Product.product_name
HAVING
    MIN(Sales.sale_date) >= '2019-01-01' 
    AND MAX(Sales.sale_date) <= '2019-03-31'

21 - 2025-08-02 07:56:41 +0000 UTC

Project Employees I

Code

SELECT 
    project_id,
    ROUND(AVG(experience_years), 2) AS average_years
FROM 
    Project
JOIN 
    Employee
ON 
    Project.employee_id = Employee.employee_id
GROUP BY 
    project_id

22 - 2025-08-02 07:53:44 +0000 UTC

Project Employees I

Code

SELECT 
    project_id,
    ROUND(AVG(experience_years), 2) AS average_years
FROM 
    Project
JOIN 
    Employee
ON 
    Project.employee_id = Employee.employee_id
GROUP BY 
    project_id

23 - 2025-08-02 07:46:44 +0000 UTC

Product Sales Analysis I

Code

SELECT
    product_name, year, price
FROM 
    Sales
LEFT JOIN
    Product
ON
    Sales.product_id = Product.product_id

24 - 2025-08-02 07:41:17 +0000 UTC

Actors and Directors Who Cooperated At Least Three Times

Code

SELECT 
    actor_id, director_id
FROM 
    ActorDirector
GROUP BY 
    actor_id, director_id
HAVING 
    COUNT(actor_id) >= 3;

25 - 2025-07-30 17:52:44 +0000 UTC

Swap Salary

Code

UPDATE
    Salary
SET
    sex = (
        CASE
            WHEN
                sex = 'f'
            THEN
                'm'
            ELSE
                'f'
        END
    );

26 - 2025-07-30 17:47:51 +0000 UTC

Not Boring Movies

Code

SELECT
    id, movie, description, rating
FROM
    Cinema
WHERE
    description != 'boring'
    AND id % 2 != 0
ORDER BY
    rating DESC;

27 - 2025-07-30 17:42:31 +0000 UTC

Biggest Single Number

Code

WITH nums AS (
    SELECT 
        num
    FROM 
        MyNumbers
    GROUP BY 
        num
    HAVING 
        COUNT(num) = 1
) 
SELECT 
    MAX(num) AS num
FROM 
    nums;

28 - 2025-07-30 17:12:39 +0000 UTC

Triangle Judgement

Code

SELECT
    x, y, z, ( 
        CASE
            WHEN 
                ((x+y+z) - GREATEST(x,y,z)) > GREATEST(x,y,z) 
            THEN 
                'Yes' 
            ELSE 
                'No'
        END
    ) AS triangle
FROM 
    Triangle;

29 - 2025-07-30 17:09:55 +0000 UTC

Triangle Judgement

Code

(
    SELECT
        x, y, z, 'Yes' AS triangle
    FROM
        Triangle
    WHERE 
        (x + y > z) AND (y + z > x) AND (z + x > y)
) UNION (
    SELECT 
        x, y, z, 'No' AS triangle
    FROM
        Triangle
    WHERE (x + y <= z) OR (y + z <= x) OR (z + x <= y)
);

30 - 2025-07-30 17:05:18 +0000 UTC

Sales Person

Code

SELECT 
    sales_person.name
FROM (
    Orders orders
    JOIN 
        Company company
    ON 
        orders.com_id = company.com_id
        AND company.name = 'RED'
    RIGHT JOIN 
        SalesPerson sales_person
    ON 
        sales_person.sales_id = orders.sales_id
)
WHERE
    orders.sales_id IS NULL;

31 - 2025-07-30 16:18:29 +0000 UTC

Classes With at Least 5 Students

Code

SELECT 
    class
FROM 
    Courses
GROUP BY 
    class
HAVING 
    COUNT(class) >= 5;

32 - 2025-07-30 16:14:46 +0000 UTC

Big Countries

Code

SELECT
    World.name, World.population, World.area
FROM
    World
WHERE
    World.area >= 3000000
    OR World.population >= 25000000;

33 - 2025-07-30 15:32:15 +0000 UTC

Customer Placing the Largest Number of Orders

Code

SELECT 
    Orders.customer_number
FROM 
    Orders
GROUP BY 
    Orders.customer_number
ORDER BY 
    COUNT(Orders.customer_number) DESC
LIMIT 
    1;

34 - 2025-07-30 15:31:28 +0000 UTC

Customer Placing the Largest Number of Orders

Code

SELECT 
    Orders.customer_number
FROM 
    Orders
GROUP BY 
    Orders.customer_number
ORDER BY 
    COUNT(*) DESC
LIMIT 
    1;

35 - 2025-07-30 15:25:43 +0000 UTC

Find Customer Referee

Code

SELECT
    Customer.name
FROM 
    Customer
WHERE
    Customer.referee_id != 2
    OR Customer.referee_id IS NULL

36 - 2025-07-30 15:19:10 +0000 UTC

Employee Bonus

Code

SELECT
    Employee.name, Bonus.bonus
FROM
    Employee
LEFT JOIN
    Bonus
ON 
    Employee.empId = Bonus.empId
WHERE
    Bonus.bonus IS NULL
    OR Bonus.bonus < 1000

37 - 2025-07-29 18:27:41 +0000 UTC

Game Play Analysis I

Code

SELECT
    player_id, MIN(event_date) as "first_login"
FROM
    Activity
GROUP BY
    player_id

38 - 2025-07-29 13:37:09 +0000 UTC

Delete Duplicate Emails

Code

DELETE FROM
    Person
WHERE 
    id NOT IN (
        SELECT MIN(id) FROM Person 
        GROUP BY email
    )
;

39 - 2025-07-29 13:11:49 +0000 UTC

Rising Temperature

Code

SELECT 
    today.id
FROM 
    Weather today
CROSS JOIN 
    Weather yesterday
WHERE 
    (today.recordDate - yesterday.recordDate = 1)
    AND today.temperature > yesterday.temperature

40 - 2025-07-28 17:06:14 +0000 UTC

Employees Earning More Than Their Managers

Code

SELECT employee.name AS Employee
FROM Employee employee
JOIN Employee manager
ON employee.managerId = manager.id
WHERE employee.salary > manager.salary;

41 - 2025-07-28 13:45:20 +0000 UTC

Customers Who Never Order

Code

SELECT name as "Customers"
FROM Customers
WHERE id NOT IN (
    SELECT customerId 
    FROM Orders
);

42 - 2025-07-28 13:35:55 +0000 UTC

Duplicate Emails

Code

SELECT email FROM Person
GROUP BY email
HAVING COUNT(email) > 1;

43 - 2025-07-25 09:18:12 +0000 UTC

Combine Two Tables

Code

SELECT firstName, lastName, city, state 
FROM Person LEFT JOIN Address 
    ON Person.personId = Address.personId