From 6c08462522934a62c0153e6cc7b271825c75fbb2 Mon Sep 17 00:00:00 2001 From: bot50 Date: Mon, 27 Apr 2026 15:35:10 +0000 Subject: [PATCH] daviddoji-cs50/problems/2024/sql/dese@20260427T153510.985043828Z --- 1.sql | 2 ++ 10.sql | 9 +++++++++ 11.sql | 14 ++++++++++++++ 12.sql | 33 +++++++++++++++++++++++++++++++++ 13.sql | 0 2.sql | 2 ++ 3.sql | 2 ++ 4.sql | 10 ++++++++++ 5.sql | 9 +++++++++ 6.sql | 9 +++++++++ 7.sql | 8 ++++++++ 8.sql | 5 +++++ 9.sql | 8 ++++++++ 13 files changed, 111 insertions(+) create mode 100644 1.sql create mode 100644 10.sql create mode 100644 11.sql create mode 100644 12.sql create mode 100644 13.sql create mode 100644 2.sql create mode 100644 3.sql create mode 100644 4.sql create mode 100644 5.sql create mode 100644 6.sql create mode 100644 7.sql create mode 100644 8.sql create mode 100644 9.sql diff --git a/1.sql b/1.sql new file mode 100644 index 0000000..1b8a098 --- /dev/null +++ b/1.sql @@ -0,0 +1,2 @@ +-- write a SQL query to find the names and cities of all public schools in Massachusetts. +SELECT "name", "city" FROM "schools" WHERE "type" = "Public School"; diff --git a/10.sql b/10.sql new file mode 100644 index 0000000..1d86f23 --- /dev/null +++ b/10.sql @@ -0,0 +1,9 @@ +-- write a SQL query to find the 10 public school districts with the highest per-pupil expenditures. +-- Your query should return the names of the districts and the per-pupil expenditure for each. +SELECT d."name", e."per_pupil_expenditure" +FROM "expenditures" AS e +JOIN "districts" AS d +ON e."district_id" = d."id" +WHERE d."type" = 'Public School District' +ORDER BY e."per_pupil_expenditure" DESC +LIMIT 10; diff --git a/11.sql b/11.sql new file mode 100644 index 0000000..a0007a7 --- /dev/null +++ b/11.sql @@ -0,0 +1,14 @@ +-- write a SQL query to display the names of schools, their per-pupil expenditure, and their graduation rate. +-- Sort the schools from greatest per-pupil expenditure to least. +-- If two schools have the same per-pupil expenditure, sort by school name. +-- +-- You should assume a school spends the same amount per-pupil their district as a whole spends. +SELECT s."name", e."per_pupil_expenditure", g."graduated" +FROM "schools" AS s +JOIN "districts" AS d + ON s."district_id" = d."id" +JOIN "expenditures" AS e + ON d."id" = e."district_id" +JOIN "graduation_rates" AS g + ON s."id" = g."school_id" +ORDER BY e."per_pupil_expenditure" DESC, s."name" ASC; diff --git a/12.sql b/12.sql new file mode 100644 index 0000000..432c14f --- /dev/null +++ b/12.sql @@ -0,0 +1,33 @@ +-- write a SQL query to find public school districts with above-average per-pupil expenditures and +-- an above-average percentage of teachers rated “exemplary”. Your query should return the districts’ names, +-- along with their per-pupil expenditures and percentage of teachers rated exemplary. Sort the results first by +-- the percentage of teachers rated exemplary (high to low), then by the per-pupil expenditure (high to low). + +-- You might find it helpful to know that subqueries can be inserted into most any part of a SQL query, +-- including conditions. For instance, the following is valid SQL syntax: + +-- SELECT "column" FROM "table" +-- WHERE "column" > ( +-- SELECT AVG("column") +-- FROM "table" +-- ); + +SELECT + d."name", + e."per_pupil_expenditure", + s."exemplary" +FROM "districts" AS d +JOIN "expenditures" AS e + ON d."id" = e."district_id" +JOIN "staff_evaluations" AS s + ON d."id" = s."district_id" +WHERE d."type" = 'Public School' + AND e."per_pupil_expenditure" > ( + SELECT AVG("per_pupil_expenditure") + FROM "expenditures" + ) + AND s."exemplary" > ( + SELECT AVG("exemplary") + FROM "staff_evaluations" + ) +ORDER BY s."exemplary" DESC, e."per_pupil_expenditure" DESC; diff --git a/13.sql b/13.sql new file mode 100644 index 0000000..e69de29 diff --git a/2.sql b/2.sql new file mode 100644 index 0000000..7b0a4ca --- /dev/null +++ b/2.sql @@ -0,0 +1,2 @@ +-- write a SQL query to find the names of districts that are no longer operational. +SELECT "name" FROM "districts" WHERE "name" LIKE "%(non-op)"; diff --git a/3.sql b/3.sql new file mode 100644 index 0000000..bc2d051 --- /dev/null +++ b/3.sql @@ -0,0 +1,2 @@ +-- write a SQL query to find the average per-pupil expenditure. Name the column “Average District Per-Pupil Expenditure”. +SELECT AVG("per_pupil_expenditure") AS "Average District Per-Pupil Expenditure" FROM "expenditures"; diff --git a/4.sql b/4.sql new file mode 100644 index 0000000..c614136 --- /dev/null +++ b/4.sql @@ -0,0 +1,10 @@ +-- write a SQL query to find the 10 cities with the most public schools. +-- Your query should return the names of the cities and the number of public schools within them, +-- ordered from greatest number of public schools to least. If two cities have the same number of public schools, +-- order them alphabetically. +SELECT "city", COUNT(*) AS "Number of schools" +FROM "schools" +WHERE "type" = "Public School" +GROUP BY "city" +ORDER BY "Number of schools" DESC, "city" ASC +LIMIT 10; diff --git a/5.sql b/5.sql new file mode 100644 index 0000000..86540ed --- /dev/null +++ b/5.sql @@ -0,0 +1,9 @@ +-- write a SQL query to find cities with 3 or fewer public schools. Your query should return the names of the cities +-- and the number of public schools within them, ordered from greatest number of public schools to least. +-- If two cities have the same number of public schools, order them alphabetically. +SELECT "city", COUNT(*) AS "Number of schools" +FROM "schools" +WHERE "type" = "Public School" +GROUP BY "city" +HAVING COUNT(*) <= 3 +ORDER BY "Number of schools" DESC, "city" ASC; diff --git a/6.sql b/6.sql new file mode 100644 index 0000000..1e62ab7 --- /dev/null +++ b/6.sql @@ -0,0 +1,9 @@ +-- write a SQL query to find the names of schools (public or charter!) that reported a 100% graduation rate. +SELECT "name" +FROM "schools" +WHERE "id" IN ( + SELECT "school_id" + FROM "graduation_rates" + WHERE "graduated" = 100 + ) +; diff --git a/7.sql b/7.sql new file mode 100644 index 0000000..c12b367 --- /dev/null +++ b/7.sql @@ -0,0 +1,8 @@ +-- write a SQL query to find the names of schools (public or charter!) in the Cambridge school district. +-- Keep in mind that Cambridge, the city, contains a few school districts, but DESE is interested in the +-- district whose name is “Cambridge.” +SELECT s."name" +FROM "schools" AS s +JOIN "districts" AS d + ON s."district_id" = d."id" +WHERE d."name" = 'Cambridge'; diff --git a/8.sql b/8.sql new file mode 100644 index 0000000..de72719 --- /dev/null +++ b/8.sql @@ -0,0 +1,5 @@ +-- write a SQL query to display the names of all school districts and the number of pupils enrolled in each. +SELECT d."name", e."pupils" +FROM "expenditures" AS e +JOIN "districts" AS d +ON e."district_id" = d."id"; diff --git a/9.sql b/9.sql new file mode 100644 index 0000000..fd131bc --- /dev/null +++ b/9.sql @@ -0,0 +1,8 @@ +-- write a SQL query to find the name (or names) of the school district(s) with the single least number of pupils. +-- Report only the name(s). +SELECT d."name" +FROM "expenditures" AS e +JOIN "districts" AS d +ON e."district_id" = d."id" +ORDER BY "pupils" ASC +LIMIT 1;