Skip to content

Commit 67d2b43

Browse files
committed
today Sql work
1 parent 3cfeded commit 67d2b43

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

.gitattributes

+5
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
*.sql linguist-detectable=true
22
*.sql linguist-language=SQL
3+
4+
5+
*.md linguist-vendored
6+
*.json linguist-vendored
7+
*.py linguist-vendored

1075. Project Employees I.sql

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
1075. Project Employees I
2+
Solved
3+
Easy
4+
Topics
5+
Companies
6+
SQL Schema
7+
Pandas Schema
8+
Table: Project
9+
10+
+-------------+---------+
11+
| Column Name | Type |
12+
+-------------+---------+
13+
| project_id | int |
14+
| employee_id | int |
15+
+-------------+---------+
16+
(project_id, employee_id) is the primary key of this table.
17+
employee_id is a foreign key to Employee table.
18+
Each row of this table indicates that the employee with employee_id is working on the project with project_id.
19+
20+
21+
Table: Employee
22+
23+
+------------------+---------+
24+
| Column Name | Type |
25+
+------------------+---------+
26+
| employee_id | int |
27+
| name | varchar |
28+
| experience_years | int |
29+
+------------------+---------+
30+
employee_id is the primary key of this table. It's guaranteed that experience_years is not NULL.
31+
Each row of this table contains information about one employee.
32+
33+
34+
Write an SQL query that reports the average experience years of all the employees for each project, rounded to 2 digits.
35+
36+
Return the result table in any order.
37+
38+
The query result format is in the following example.
39+
40+
41+
42+
Example 1:
43+
44+
Input:
45+
Project table:
46+
+-------------+-------------+
47+
| project_id | employee_id |
48+
+-------------+-------------+
49+
| 1 | 1 |
50+
| 1 | 2 |
51+
| 1 | 3 |
52+
| 2 | 1 |
53+
| 2 | 4 |
54+
+-------------+-------------+
55+
Employee table:
56+
+-------------+--------+------------------+
57+
| employee_id | name | experience_years |
58+
+-------------+--------+------------------+
59+
| 1 | Khaled | 3 |
60+
| 2 | Ali | 2 |
61+
| 3 | John | 1 |
62+
| 4 | Doe | 2 |
63+
+-------------+--------+------------------+
64+
Output:
65+
+-------------+---------------+
66+
| project_id | average_years |
67+
+-------------+---------------+
68+
| 1 | 2.00 |
69+
| 2 | 2.50 |
70+
+-------------+---------------+
71+
Explanation: The average experience years for the first project is (3 + 2 + 1) / 3 = 2.00 and for the second project is (3 + 2) / 2 = 2.50
72+
73+
'''SQL
74+
75+
# Write your MySQL query statement below
76+
select project_id , round(avg(experience_years), 2) as average_years
77+
from project as p
78+
left join employee as e
79+
on p.employee_id = e.employee_id
80+
group by project_id;
81+
82+
'''

1251. Average Selling Price.sql

+7-5
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96
7575
Note that the average selling price is 0 if the product was not sold.
7676

7777
# Write your MySQL query statement below
78-
SELECT a.product_id,ROUND(SUM(b.units*a.price)/SUM(b.units),2) as average_price
79-
FROM Prices as a
80-
JOIN UnitsSold as b
81-
ON a.product_id=b.product_id AND (b.purchase_date BETWEEN a.start_date AND a.end_date)
82-
GROUP BY product_id;
8378

79+
# Write your MySQL query statement below
80+
SELECT p.product_id, IFNULL(round(SUM(p.price*u.units)/sum(u.units),2),0) as average_price
81+
FROM Prices p
82+
LEFT JOIN UnitsSold u
83+
ON p.product_id = u.product_id AND
84+
u.purchase_date BETWEEN p.Start_date and p.end_date
85+
GROUP BY p.product_id
8486
-- SELECT product_id, ROUND(AVG(COALESCE(price, 0)), 2) AS average_price

0 commit comments

Comments
 (0)