├── 01-projectList ├── README.md └── projectList.sql ├── 02-countriesSelection ├── README.md └── countriesSelection.sql ├── 03-monthlyScholarships ├── README.md └── monthlyScholarships.sql ├── 04-projectsTeam ├── README.md └── projectsTeam.sql ├── 05-automaticNotifications ├── README.md └── automaticNotifications.sql ├── 06-volleyballResults ├── README.md └── volleyballResults.sql ├── 07-mostExpensive ├── README.md └── mostExpensive.sql ├── 08-contestLeaderboard ├── README.md └── contestLeaderboard.sql ├── 09-gradeDistribution ├── README.md └── gradeDistribution.sql ├── 10-mischievousNephews ├── README.md └── mischievousNephews.sql ├── 11-suspectsInvestigation ├── README.md └── suspectsInvestigation.sql ├── 12-suspectsInvestigation2 ├── README.md └── suspectsInvestigation2.sql ├── 13-securityBreach ├── README.md └── securityBreach.sql ├── 14-testCheck ├── README.md └── testCheck.sql ├── 15-expressionsVerification ├── README.md └── expressionsVerification.sql ├── 16-newsSubscribers ├── README.md └── newsSubscribers.sql ├── 17-countriesInfo ├── README.md └── countriesInfo.sql ├── 18-itemCounts ├── README.md └── itemCounts.sql ├── 19-usersByContinent ├── README.md └── usersByContinent.sql ├── 20-movieDirectors ├── README.md └── movieDirectors.sql ├── 21-travelDiary ├── README.md └── travelDiary.sql ├── 22-soccerPlayers ├── README.md └── soccerPlayers.sql ├── 23-marketReport ├── README.md └── marketReport.sql ├── 24-websiteHacking ├── README.md └── websiteHacking.sql ├── 25-nullIntern ├── README.md └── nullIntern.sql ├── 26-legsCount ├── README.md └── legsCount.sql ├── 27-combinationLock ├── README.md └── combinationLock.sql ├── 28-interestClub ├── README.md └── interestClub.sql ├── 29-personalHobbies ├── README.md └── personalHobbies.sql ├── 30-booksCatalogs ├── README.md └── booksCatalogs.sql ├── 31-habitatArea ├── README.md └── habitatArea.sql ├── 32-orderOfSuccession ├── README.md └── orderOfSuccession.sql ├── 33-orderingEmails ├── README.md └── orderingEmails.sql ├── 34-placesOfInterest ├── README.md └── placesOfInterest.sql ├── 35-soccerGameSeries ├── README.md └── soccerGameSeries.sql ├── 36-correctIPs ├── README.md └── correctIPs.sql ├── 37-validPhoneNumbers ├── README.md └── validPhoneNumbers.sql ├── 38-importantEvents ├── README.md └── importantEvents.sql ├── 39-dateFormatting ├── README.md └── dateFormatting.sql ├── 40-pastEvents ├── README.md └── pastEvents.sql ├── 41-netIncome ├── README.md └── netIncome.sql ├── 42-alarmClocks ├── README.md └── alarmClocks.sql ├── 43-companyEmployees ├── README.md └── companyEmployees.sql ├── 44-scholarshipsDistribution ├── README.md └── scholarshipsDistribution.sql ├── 45-userCountries ├── README.md └── userCountries.sql ├── 46-placesOfInterestPairs ├── README.md └── placesOfInterestPairs.sql ├── 47-localCalendar ├── README.md └── localCalendar.sql ├── 48-routeLength ├── README.md └── routeLength.sql ├── 49-currencyCodes ├── README.md └── currencyCodes.sql ├── 50-coursesDistribution ├── README.md └── coursesDistribution.sql ├── 51-nicknames ├── README.md └── nicknames.sql ├── 52-tableSecurity ├── README.md └── tableSecurity.sql ├── 53-officeBranches ├── README.md └── officeBranches.sql ├── 54-restaurantInfo ├── README.md └── restaurantInfo.sql ├── 55-studentsInClubs ├── README.md └── studentsInClubs.sql ├── 56-emptyDepartments ├── README.md └── emptyDepartments.sql ├── 57-sunnyHolidays ├── README.md └── sunnyHolidays.sql ├── 58-closestCells ├── README.md └── closestCells.sql ├── 59-top5AverageGrade ├── README.md └── top5AverageGrade.sql ├── 60-salaryDifference ├── README.md └── salaryDifference.sql ├── 61-recentHires ├── README.md └── recentHires.sql ├── 62-checkExpenditure ├── README.md └── checkExpenditure.sql ├── 63-dancingCompetition ├── README.md └── dancingCompetition.sql ├── 64-trackingSystem ├── README.md └── trackingSystem.sql ├── 65-storageOptimization ├── README.md └── storageOptimization.sql ├── 66-consecutiveIds ├── README.md └── consecutiveIds.sql ├── 67-holidayEvent ├── README.md └── holidayEvent.sql ├── 68-hostnamesOrdering ├── README.md └── hostnamesOrdering.sql ├── 69-orderAnalytics ├── README.md └── orderAnalytics.sql ├── 70-customerMessages ├── README.md └── customerMessages.sql ├── 71-orderPrices ├── README.md └── orderPrices.sql ├── 72-findTable ├── README.md └── findTable.sql ├── 73-queriesExecution ├── README.md └── queriesExecution.sql ├── 74-filmLibrary ├── README.md └── filmLibrary.sql ├── 75-bugsInComponent ├── README.md └── bugsInComponent.sql ├── 76-freeSeats ├── README.md └── freeSeats.sql ├── 77-giftPackaging ├── README.md └── giftPackaging.sql ├── 78-stringsStatistics ├── README.md └── stringsStatistics.sql ├── 79-unluckyEmployees ├── README.md └── unluckyEmployees.sql ├── 80-driversInfo ├── README.md └── driversInfo.sql ├── 81-sortBookChapters ├── README.md └── sortBookChapters.sql ├── 82-typeInheritance ├── README.md └── typeInheritance.sql ├── 83-battleshipGameResults ├── README.md └── battleshipGameResults.sql └── 84-tictactoeTournament ├── README.md └── tictactoeTournament.sql /01-projectList/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 |
Your boss wants to identify the successful projects running in your company, so he asked you to prepare a list of all the currently active projects and their average monthly income.
4 |You have stored the information about these projects in a simple database with a single Projects table that has five columns:
5 |Your boss says that internal project ids are irrelevant to him and that he isn't interested in how big the teams are. Since that's the case, he wants you to create another table by removing the internal_id and team_size columns from the existing Projects table. Return it sorted by internal_id
in ascending order.
Example
14 |For the following table Projects
15 |internal_id | 18 |project_name | 19 |team_size | 20 |team_lead | 21 |income | 22 |
---|---|---|---|---|
1384 | 25 |MapReduce | 26 |100 | 27 |Jeffrey Dean | 28 |0 | 29 |
2855 | 32 |Windows | 33 |1000 | 34 |Bill Gates | 35 |100500 | 36 |
5961 | 39 |Snapchat | 40 |3 | 41 |Evan Spiegel | 42 |2000 | 43 |
project_name | 49 |team_lead | 50 |income | 51 |
---|---|---|
MapReduce | 54 |Jeffrey Dean | 55 |0 | 56 |
Windows | 59 |Bill Gates | 60 |100500 | 61 |
Snapchat | 64 |Evan Spiegel | 65 |2000 | 66 |
source: codesignal.com 3 |
Your friend wants to become a professional tour guide and travel all around the world. In pursuit of this dream, she enrolled in tour guide school. The professors in this school turned out to be very demanding, and one of them gave your friend a difficult assignment that she has to finish over the weekend.
4 |Here's the assignment: Given a list of countries, your friend should identify all the countries that are in Africa. To help her, you have decided to write a function that will find all such countries from any set of countries. The countries table in which the countries are stored has the following structure:
5 |name
: the name of the country;continent
: the continent on which the country is situated;population
: the country's population.Your task is to return a new table that has the same columns, but that only contains the countries from Africa. The countries should be sorted alphabetically by their names.
11 |Example
12 |For the following table countries
13 |name | 16 |continent | 17 |population | 18 |
---|---|---|
Austria | 21 |Europe | 22 |8767919 | 23 |
Belize | 26 |North America | 27 |375909 | 28 |
Botswana | 31 |Africa | 32 |2230905 | 33 |
Cambodia | 36 |Asia | 37 |15626444 | 38 |
Cameroon | 41 |Africa | 42 |22709892 | 43 |
the output should be
46 |name | 49 |continent | 50 |population | 51 |
---|---|---|
Botswana | 54 |Africa | 55 |2230905 | 56 |
Cameroon | 59 |Africa | 60 |22709892 | 61 |
source: codesignal.com 3 |
Students at your university get scholarships that are paid out throughout the year.
4 |Information about the scholarships is stored in the table scholarships, which has the structure:
5 |id
: the unique student id;scholarship
: the amount of the annual scholarship the student has been awarded.Now you need to calculate the amount of money each student should get per month. Given the table scholarships, build the resulting table as follows: The table should have the same columns as the initial table, but the scholarship
column should contain the amount of the student's monthly scholarship payout. The rows should be ordered by the students' id
s.
Example
11 |For the following table scholarships
12 |id | 15 |scholarship | 16 |
---|---|
1 | 19 |12000 | 20 |
2 | 23 |18000 | 24 |
3 | 27 |24000 | 28 |
4 | 31 |15000 | 32 |
5 | 35 |21000 | 36 |
6 | 39 |13000 | 40 |
the output should be
43 |id | 46 |scholarship | 47 |
---|---|
1 | 50 |1000 | 51 |
2 | 54 |1500 | 55 |
3 | 58 |2000 | 59 |
4 | 62 |1250 | 63 |
5 | 66 |1750 | 67 |
6 | 70 |1083.3333333333333 | 71 |
source: codesignal.com 3 |
You've been promoted and assigned to a new project. The problem is, you don't know who you are working with and your predecessor has vanished without a trace! Luckily, each project in your company keeps its own activity database, which you are going to use to find out the names of your new colleagues.
4 |Information about the project's activity is stored in table projectLog, which has the following structure:
5 |id
: unique action id;name
: the name of the person who performed the action;description
: the description of the action;timestamp
: the timestamp of the action.You only have access to the project's most recent history, but this should be enough for you. You've decided that finding everyone who interacted with the project in this period is the best way to start.
12 |Given the table projectLog, build a new results table with a single name
column that contains the names of the project's contributors sorted in ascending order.
Example
14 |For the following table projectLog
15 |id | 18 |name | 19 |description | 20 |timestamp | 21 |
---|---|---|---|
1 | 24 |James Smith | 25 |add new logo | 26 |2015-11-10 15:24:32 | 27 |
2 | 30 |John Johnson | 31 |update license | 32 |2015-11-10 15:50:01 | 33 |
3 | 36 |John Johnson | 37 |fix typos | 38 |2015-11-10 15:55:01 | 39 |
4 | 42 |James Smith | 43 |update logo | 44 |2015-11-10 17:53:04 | 45 |
5 | 48 |James Smith | 49 |delete old logo | 50 |2015-11-10 17:54:04 | 51 |
6 | 54 |Michael Williams | 55 |fix the build | 56 |2015-11-12 13:37:00 | 57 |
7 | 60 |Mary Troppins | 61 |add new feature | 62 |2015-11-08 17:54:04 | 63 |
8 | 66 |James Smith | 67 |fix fonts | 68 |2015-11-14 13:54:04 | 69 |
9 | 72 |Richard Young | 73 |remove unneeded files | 74 |2015-11-14 00:00:00 | 75 |
10 | 78 |Michael Williams | 79 |add tests | 80 |2015-11-09 11:53:00 | 81 |
the output should be
84 |name | 87 |
---|
James Smith | 90 |
John Johnson | 93 |
Mary Troppins | 96 |
Michael Williams | 99 |
Richard Young | 102 |
source: codesignal.com 3 |
The application you've been working on for the past year is a huge success! It already has a large and active user community. You know the ID number, username, and email of each user. Each user also has a specific role that shows their position in the community. Information about the users is stored in the database as a table users, which has the following structure:
4 |id
: the unique user ID;username
: the username of the user;role
the user's role;email
: the user's email.You want to send users automatic notifications to let them know about the most recent updates. However, not all users should get these notifications: Administrators don't need notifications since they know about the updates already, and premium users don't need them since they get personalized weekly updates.
11 |Given the users table, your task is to return the emails of all the users who should get notifications, i.e. those whose role
is not equal to "admin"
or "premium"
. Note that roles are case insensitive, so users with role
s of "Admin"
, "pReMiUm"
, etc. should also be excluded.
The resulting table should contain a single email
column and be sorted by email
s in ascending order.
Example
14 |For the following table users
15 |id | 18 |username | 19 |role | 20 ||
---|---|---|---|
6 | 24 |fasalytch | 25 |premium | 26 |much.premium@role.com | 27 |
13 | 30 |luckygirl | 31 |regular | 32 |fun@meh.com | 33 |
16 | 36 |todayhumor | 37 |guru | 38 |today@humor.com | 39 |
23 | 42 |Felix | 43 |admin | 44 |felix@codesignal.com | 45 |
52 | 48 |admin666 | 49 |AdmiN | 50 |iamtheadmin@admin.admin | 51 |
87 | 54 |solver100500 | 55 |regular | 56 |email@notbot.com | 57 |
the resulting table should be
60 |email@notbot.com | 66 |
fun@meh.com | 69 |
today@humor.com | 72 |
The only three users who should get notifications are luckygirl
, todayhumor
, and solver100500
. Their emails are fun@meh.com
, today@humor.com
, and email@notbot.com
respectively, which should be sorted as email@notbot.com
, fun@meh.com
, and today@humor.com
.
source: codesignal.com 3 |
You are creating a website that will help you and your friends keep track of the results of volleyball teams from all around the world. Your website regularly crawls the web searching for new games, and adds the results of these games to the results table stored in your local database. After each update, the table should be sorted in ascending order by the total number of games won. This year's results are quite marvelous - at any given moment there are no two teams that have won the same number of games!
4 |The results table contains the following columns:
5 |name
- the unique name of the team;country
- the country of the team;scored
- the number of scored goals;missed
- the number of missed goals;wins
- the total number of games the team has won.Your task is to sort the given results table in ascending order by the number of wins
.
Example
14 |For given table results
15 |name | 18 |country | 19 |scored | 20 |missed | 21 |wins | 22 |
---|---|---|---|---|
FC Tokyo | 25 |Japan | 26 |26 | 27 |28 | 28 |1 | 29 |
Fujian | 32 |China | 33 |24 | 34 |26 | 35 |0 | 36 |
Jesus Maria | 39 |Argentina | 40 |25 | 41 |23 | 42 |3 | 43 |
University Blues | 46 |Australia | 47 |16 | 48 |25 | 49 |2 | 50 |
the output should be
53 |name | 56 |country | 57 |scored | 58 |missed | 59 |wins | 60 |
---|---|---|---|---|
Fujian | 63 |China | 64 |24 | 65 |26 | 66 |0 | 67 |
FC Tokyo | 70 |Japan | 71 |26 | 72 |28 | 73 |1 | 74 |
University Blues | 77 |Australia | 78 |16 | 79 |25 | 80 |2 | 81 |
Jesus Maria | 84 |Argentina | 85 |25 | 86 |23 | 87 |3 | 88 |
source: codesignal.com 3 |
Mr. Cash wants to keep track of his expenses, so he has prepared a list of all the products he bought this month. Now he is interested in finding the product on which he spent the largest amount of money. If there are products that cost the same amount of money, he'd like to find the one with the lexicographically smallest name.
4 |The list of expenses is stored in a table Products which has the following columns:
5 |id
: unique product id;name
: the unique name of the product;price
: the price for one item;quantity
: the number of items bought.The resulting table should contain one row with a single column: the product with the lexicographically smallest name on which Mr. Cash spent the largest amount of money.
12 |The total amount of money spent on a product is calculated as price * quantity
.
Example
14 |id | 20 |name | 21 |price | 22 |quantity | 23 |
---|---|---|---|
1 | 26 |MacBook Air | 27 |1500 | 28 |1 | 29 |
2 | 32 |Magic Mouse | 33 |79 | 34 |1 | 35 |
3 | 38 |Spray cleaner | 39 |10 | 40 |3 | 41 |
the output should be
name | 47 |
---|
MacBook Air | 50 |
id | 58 |name | 59 |price | 60 |quantity | 61 |
---|---|---|---|
1 | 64 |Tomato | 65 |10 | 66 |4 | 67 |
2 | 70 |Cucumber | 71 |8 | 72 |5 | 73 |
3 | 76 |Red Pepper | 77 |20 | 78 |2 | 79 |
4 | 82 |Feta | 83 |40 | 84 |1 | 85 |
the output should be
name | 91 |
---|
Cucumber | 94 |
While the total cost for each product was 40
, Cucumber
has the lexicographically smallest name.
source: codesignal.com 3 |
You are working as a recruiter at a big IT company, and you're actively looking for candidates who take the top places in major programming contests. Since the grand finale of the annual City Competition, you've been reaching out to the top participants from the leaderboard, and successfully so.
4 |You have already interviewed all the prize winners (the top 3
participants), but that's not enough right now. Your company needs more specialists, so now you would like to connect with the participants who took the next 5
places.
The contest leaderboard is stored in a table leaderboard with the following columns:
6 |id
: unique id of the participant;name
: the name of the participant;score
: the score the participant achieved in the competition.The resulting table should contain the names of the participants who took the 4th
to 8th
places inclusive, sorted in descending order of their places. If there are fewer than 8
participants, the results should contain those who ranked lower than 3rd
place.
It is guaranteed that there are at least 3 prize winners in the leaderboard and that all participants have different scores.
13 |Example
14 |For the following table leaderboard
15 |id | 18 |name | 19 |score | 20 |
---|---|---|
1 | 23 |gongy | 24 |3001 | 25 |
2 | 28 |urandom | 29 |2401 | 30 |
3 | 33 |eduardische | 34 |2477 | 35 |
4 | 38 |Gassa | 39 |2999 | 40 |
5 | 43 |bcc32 | 44 |2658 | 45 |
6 | 48 |Alex_2oo8 | 49 |6000 | 50 |
7 | 53 |mirosuaf | 54 |2479 | 55 |
8 | 58 |Sparik | 59 |2399 | 60 |
9 | 63 |thomas_holmes | 64 |2478 | 65 |
10 | 68 |cthaeghya | 69 |2400 | 70 |
the output should be
73 |name | 76 |
---|
bcc32 | 79 |
mirosuaf | 82 |
thomas_holmes | 85 |
eduardische | 88 |
urandom | 91 |
source: codesignal.com 3 |
At the end of every semester your professor for "Introduction to Databases" saves the exam results of every student in a simple database system. In the database table Grades, there are five columns:
4 |According to school policy, there are three possible ways to evaluate a grade:
12 |Each student's final grade comes from the option that works the best for that student.
33 |As a Teaching Assistant (TA), you need to query the name and id of all the students whose best grade comes from Option 3, sorted based on the first 3 characters of their name. If the first 3 characters of two names are the same, then the student with the lower ID value comes first.
34 |Example
35 |For the following table Grades
36 |Name | 39 |ID | 40 |Midterm1 | 41 |Midterm2 | 42 |Final | 43 |
David | 46 |42334 | 47 |34 | 48 |54 | 49 |124 | 50 |
Anthony | 53 |54528 | 54 |100 | 55 |10 | 56 |50 | 57 |
Jonathan | 60 |58754 | 61 |49 | 62 |58 | 63 |121 | 64 |
Jonty | 67 |11000 | 68 |25 | 69 |30 | 70 |180 | 71 |
Output should be
74 |Name | 77 |ID | 78 |
David | 81 |42334 | 82 |
Jonty | 85 |11000 | 86 |
Jonathan | 89 |58754 | 90 |
For David, Jonty and Jonathan, the best option is number 3. But Anthony's best option is the second one, because Option1 = 25% of 100 + 25% of 10 +50% of 50 = 52.5, Option2 = 50% of 100 + 50% of 10 = 55, Option3 = 100% of 50 = 50.
93 |source: codesignal.com 3 |
Your nephews Huey, Dewey, and Louie are staying with you over the winter holidays. Ever since they arrived, you've hardly had a day go by without some kind of incident - the little rascals do whatever they please! Actually, you're not even mad; the ideas they come up with are pretty amazing, and it looks like there's even a system to their mischief.
4 |You decided to track and analyze their behavior, so you created the mischief table in your local database. The table has the following columns:
5 |mischief_date
: the date of the mischief (of the date
type);author
: the nephew who caused the mischief ("Huey"
, "Dewey"
or "Louie"
);title
: the title of the mischief.It looks like each of your nephews is active on a specific day of the week. You decide to check your theory by creating another table as follows:
11 | The resulting table should contain four columns, weekday
, mischief_date
, author
, and title
, where weekday
is the weekday of mischief_date
(0
for Monday, 1
for Tuesday, and so on, with 6
for Sunday). The table should be sorted by the weekday
column, and for each weekday
Huey's mischief should go first, Dewey's should go next, and Louie's should go last. In case of a tie, mischief_date
should be a tie-breaker. If there's still a tie, the record with the lexicographically smallest title
should go first.
It is guaranteed that all entries of mischief are unique.
13 |Example
14 |For the following table mischief
15 |mischief_date | 18 |author | 19 |title | 20 |
---|---|---|
2016-12-01 | 23 |Dewey | 24 |Cook the golden fish in a bucket | 25 |
2016-12-01 | 28 |Dewey | 29 |Paint the walls pink | 30 |
2016-12-01 | 33 |Huey | 34 |Eat all the candies | 35 |
2016-12-01 | 38 |Louie | 39 |Wrap the cat in toilet paper | 40 |
2016-12-08 | 43 |Louie | 44 |Play hockey on linoleum | 45 |
2017-01-01 | 48 |Huey | 49 |Smash a window | 50 |
2017-02-06 | 53 |Dewey | 54 |Create a rink on the porch | 55 |
the output should be
58 |weekday | 61 |mischief_date | 62 |author | 63 |title | 64 |
---|---|---|---|
0 | 67 |2017-02-06 | 68 |Dewey | 69 |Create a rink on the porch | 70 |
3 | 73 |2016-12-01 | 74 |Huey | 75 |Eat all the candies | 76 |
3 | 79 |2016-12-01 | 80 |Dewey | 81 |Cook the golden fish in a bucket | 82 |
3 | 85 |2016-12-01 | 86 |Dewey | 87 |Paint the walls pink | 88 |
3 | 91 |2016-12-01 | 92 |Louie | 93 |Wrap the cat in toilet paper | 94 |
3 | 97 |2016-12-08 | 98 |Louie | 99 |Play hockey on linoleum | 100 |
6 | 103 |2017-01-01 | 104 |Huey | 105 |Smash a window | 106 |
The first and the eighth of December are Thursdays, the sixth of February is a Monday, and the first of January is a Sunday.
109 |The dates in the example are given in the format YYYY-MM-DD
.
source: codesignal.com 3 |
A large amount of money was stolen today from the main city bank, and as the chief of police it's your duty to find the robber.
4 |You store information about your suspects in the table Suspect, which has the structure:
5 |id
: unique suspect id;name
: suspect first name;surname
: suspect surname;height
: suspect height;weight
: suspect weight.You have already gathered some evidence and discovered the following clues:
13 |170cm
;"B. Gre?n"
. "B"
definitely stands for the first letter of robber's name, and "Gre?n"
is their surname. The 4th
letter of the surname is smudged by ketchup and is unreadable.To make the list of suspects smaller, you would like to filter out the suspects who can't possibly be guilty according to the information obtained from the clues. For each remaining suspect, you want to save his/her id
, name
and surname
. Please note that the information obtained from the clue should be considered case-insensitive, so for example "bill Green"
, and "Bill green"
, and "Bill Green"
should all be included in the new table.
Given the table Suspect, build the resulting table as follows: the table should have columns id
, name
and surname
and its values should be ordered by the suspects' id
s in ascending order.
Example
20 |For the following table Suspect
21 |id | 24 |name | 25 |surname | 26 |height | 27 |weight | 28 |
---|---|---|---|---|
1 | 31 |John | 32 |Doe | 33 |165 | 34 |60 | 35 |
2 | 38 |Bill | 39 |Green | 40 |170 | 41 |67 | 42 |
3 | 45 |Baelfire | 46 |Grehn | 47 |172 | 48 |70 | 49 |
4 | 52 |Bill | 53 |Gretan | 54 |165 | 55 |55 | 56 |
5 | 59 |Brendon | 60 |Grewn | 61 |150 | 62 |50 | 63 |
6 | 66 |bill | 67 |Green | 68 |169 | 69 |50 | 70 |
the output should be
73 |id | 76 |name | 77 |surname | 78 |
---|---|---|
2 | 81 |Bill | 82 |Green | 83 |
5 | 86 |Brendon | 87 |Grewn | 88 |
6 | 91 |bill | 92 |Green | 93 |
The name of the 1st
suspect doesn't start with "B"
, the 3rd
suspect is taller than 170cm
, and the surname of the 4th
suspect doesn't match the given pattern, meaning that these suspects are not included in the results.
source: codesignal.com 3 |
A large amount of money was stolen today from the main city bank, and as the chief of police it's your duty to find the robber.
4 |You store information about your suspects in the table Suspect, which has the structure:
5 |id
: unique suspect id;name
: suspect first name;surname
: suspect surname;height
: suspect height;weight
: suspect weight.You have already gathered some evidence and discovered the following clues:
13 |170cm
;"B. Gre?n"
. "B"
definitely stands for the first letter of robber's name, and "Gre?n"
is their surname. The 4th
letter of the surname is smudged by ketchup and is unreadable.The clues you've obtained allow you to let some suspects go since they can't possibly be guilty, so now you need to build a list that contains the people who can be freed based on the gathered information. For each of these people, you need to know his/her id
, name
and surname
. Please note that the information obtained from the clue should be considered case-insensitive, so for example "bill Green"
, "Bill GrEeN"
, and "Bill Green"
should all be included in the new table.
Given the table Suspect, build the resulting table as follows: the table should have columns id
, name
and surname
and its values should be ordered by the suspects' id
s in ascending order.
Example
20 |For the following table of Suspect
21 |id | 24 |name | 25 |surname | 26 |height | 27 |weight | 28 |
---|---|---|---|---|
1 | 31 |John | 32 |Doe | 33 |165 | 34 |60 | 35 |
2 | 38 |Bill | 39 |Green | 40 |170 | 41 |67 | 42 |
3 | 45 |Baelfire | 46 |Grehn | 47 |172 | 48 |70 | 49 |
4 | 52 |Bill | 53 |Gretan | 54 |185 | 55 |55 | 56 |
5 | 59 |Brendon | 60 |Grewn | 61 |180 | 62 |50 | 63 |
6 | 66 |bill | 67 |Green | 68 |172 | 69 |50 | 70 |
the output should be
73 |id | 76 |name | 77 |surname | 78 |
1 | 81 |John | 82 |Doe | 83 |
2 | 86 |Bill | 87 |Green | 88 |
4 | 91 |Bill | 92 |Gretan | 93 |
The 1st
and the 2nd
suspects are not taller than 170cm
, and the 4th
suspect's surname doesn't match the "Gre?n"
pattern, so these suspects can't be guilty.
source: codesignal.com 3 |
You are managing a large website that uses a special algorithm for user identification. In particular, it generates a unique attribute for each person based only on their first and last names and some additional metadata.
4 |After analyzing the server logs today you found out that the website security has been breached and the data of some of your users might have been compromised.
5 |The users' info is stored in the table users with the following structure:
6 |first_name
: user's first name;second_name
: user's last name;attribute
: a unique attribute string of this user.It seems that only the users those attribute
was generated by the old version of your special algorithm were affected. Such attributes have the following format (accurate to letter cases): <one or more arbitrary character>%<first name>_<second name>%<zero or more arbitrary characters>
. It's your duty now to warn the users that have these attributes about possible risks.
Given the users table, compose the resulting table consisting only of the rows that contain affected users' info. The result should be sorted by the attribute
s in ascending order.
Example
14 |For the following table users
15 |first_name | 18 |second_name | 19 |attribute | 20 |
---|---|---|
Mikel | 23 |Cover | 24 |%Mikel_Cover% | 25 |
Vicenta | 28 |Kravitz | 29 |0%Vicenta_Kravitz% | 30 |
Tosha | 33 |Cover | 34 |02VO1aJ767GF7L186lpIfBR0fQ5406Q02YcpG42LDF4Bv26 | 35 |
Shayne | 38 |Dahlquist | 39 |0R0V331K8Q7ypBi4Az3B6Nm0jCqUk%Shayne_Dahlquist%46E3O0u7t7 | 40 |
Carrol | 43 |Llanes | 44 |2mDIb1SdJne5wfH1Al32BE92r7j1d60PJ263b2vyPn3zxQ2P7sVOM26J11UT6W0Np | 45 |
Lizabeth | 48 |Cover | 49 |d1gM87S0NEHp386jXOk0aDc7w8bx4u8q7D82ff2Z4YT43iLyZ39xYbEDXMk | 50 |
Mack | 53 |Chace | 54 |fAnU49nMrmGu308627J7ne3qqqSPJDnq6dwW607lahNB5DinTR2Rkp549G7 | 55 |
Vicenta | 58 |Marchese | 59 |kUJ3N67vLB07mQL9Ai7p18cXGzjdT32r8283ZQi | 60 |
Mikel | 63 |Kravitz | 64 |PBX86iw1Ied87Z9OarE6sdSLdt%Mikel_Kravitz%W73XOY9YaOgi060r2x12D2EmD7 | 65 |
Deirdre | 68 |Chace | 69 |PBX86iw1Ied87Z9OarE6sdSLdtDeirdrelChaceW73XOY9YaOgi060r2x12D2EmD7 | 70 |
Josphine | 73 |Arzate | 74 |PwWD95BCKVYn5YD7iHLMa3HjP9tH%josphine_arzate%d2hNHNd3RpqfUREN47 | 75 |
Deirdre | 78 |Chace | 79 |ryCE5FIyS8q54A5036luzVS91j6C7P76E9X0O58htzgthuX24LG%DEirdre_Chace% | 80 |
Julietta | 83 |Beer | 84 |Tn35g5h51u7ltW946J | 85 |
the output should be
88 |first_name | 91 |second_name | 92 |attribute | 93 |
---|---|---|
Vicenta | 96 |Kravitz | 97 |0%Vicenta_Kravitz% | 98 |
Shayne | 101 |Dahlquist | 102 |0R0V331K8Q7ypBi4Az3B6Nm0jCqUk%Shayne_Dahlquist%46E3O0u7t7 | 103 |
Mikel | 106 |Kravitz | 107 |PBX86iw1Ied87Z9OarE6sdSLdt%Mikel_Kravitz%W73XOY9YaOgi060r2x12D2EmD7 | 108 |
source: codesignal.com 3 |
Your professor gave the class a bonus task: Write a program that will check the answers for the latest test. The program will be given a table answers with the following columns:
4 |id
- the unique ID of the question;correct_answer
- the correct answer to the question, given as a string;given_answer
- the answer given to the question, which can be NULL.Your task is to return the table with a column id
and a column checks
, where for each answers id
the following string should be returned:
"no answer"
if the given_answer
is empty;"correct"
if the given_answer
is the same as the correct_answer
;"incorrect"
if the given_answer
is not empty and is incorrect.Order the records in the answer table by id
.
Example
17 |For given table answers
18 |id | 21 |correct_answer | 22 |given_answer | 23 |
---|---|---|
1 | 26 |a | 27 |a | 28 |
2 | 31 |b | 32 |NULL | 33 |
3 | 36 |c | 37 |b | 38 |
the output should be
41 |id | 44 |checks | 45 |
---|---|
1 | 48 |correct | 49 |
2 | 52 |no answer | 53 |
3 | 56 |incorrect | 57 |
source: codesignal.com 3 |
You're a math teacher at an elementary school. Today you taught your class basic arithmetic operations ("+"
, "-"
, "*"
, "/"
) and now you need to give the students some homework. You have a lot of expressions in the format a <operation> b = c
, where a
, b
, and c
are some integers and operation
is one of the operations given above.
Information about these expressions is stored in the table expressions, which has the structure:
5 |id
: the unique operation id;a
: an integer;b
: an integer;operation
: one of the operations given above ("+"
, "-"
, "*"
, or "/"
);c
: an integer.The homework you're going to give is simple: For each expression, the student needs to determine whether it's correct or not, i.e. whether it's true that the expression to the left of the =
sign equals c
.
Since you have many students and checking all their answers manually is a lot of work, you want to streamline the process by automatically identifying all the expressions that are correct. Given the table expressions, build the resulting table as follows: The table should have the same columns as the initial table does, but it should only contain those rows that represent correct expressions. The rows should be ordered by id
.
Example
15 |For the following table expressions
16 |id | 19 |a | 20 |b | 21 |operation | 22 |c | 23 |
---|---|---|---|---|
1 | 26 |2 | 27 |3 | 28 |+ | 29 |5 | 30 |
2 | 33 |2 | 34 |3 | 35 |+ | 36 |6 | 37 |
3 | 40 |3 | 41 |2 | 42 |/ | 43 |1 | 44 |
4 | 47 |4 | 48 |7 | 49 |* | 50 |28 | 51 |
5 | 54 |54 | 55 |2 | 56 |- | 57 |27 | 58 |
6 | 61 |3 | 62 |0 | 63 |/ | 64 |0 | 65 |
the output should be
68 |id | 71 |a | 72 |b | 73 |operation | 74 |c | 75 |
---|---|---|---|---|
1 | 78 |2 | 79 |3 | 80 |+ | 81 |5 | 82 |
4 | 85 |4 | 86 |7 | 87 |* | 88 |28 | 89 |
Explanation:
92 |source: codesignal.com 3 |
You are managing a small newspaper subscription service. Anyone who uses it can subscribe to a large number of different newspapers for a full year or just a half year.
4 |The information about subscriptions is stored in the full_year and half_year tables, which have the following structures:
5 |id
: the unique subscription ID;newspaper
: the newspaper's name;subscriber
: the name of the subscriber.id
: the unique subscription ID;newspaper
: the newspaper's name;subscriber
: the name of the subscriber.Given the full_year and half_year tables, compose the result as follows: The resulting table should have one column subscriber
that contains all the distinct names of anyone who is subscribed to a newspaper with the word Daily
in its name. The table should be sorted in ascending order by the subscribers' first names.
Example
23 |The following tables full_year
24 |id | 27 |newspaper | 28 |subscriber | 29 |
---|---|---|
1 | 32 |The Paragon Herald | 33 |Crissy Sepe | 34 |
2 | 37 |The Daily Reporter | 38 |Tonie Moreton | 39 |
3 | 42 |Morningtide Daily | 43 |Erwin Chitty | 44 |
4 | 47 |Daily Breakfast | 48 |Tonie Moreton | 49 |
5 | 52 |Independent Weekly | 53 |Lavelle Phu | 54 |
and half_year
57 |id | 60 |newspaper | 61 |subscriber | 62 |
---|---|---|
1 | 65 |The Daily Reporter | 66 |Lavelle Phu | 67 |
2 | 70 |Daily Breakfast | 71 |Tonie Moreton | 72 |
3 | 75 |The Paragon Herald | 76 |Lia Cover | 77 |
4 | 80 |The Community Gazette | 81 |Lavelle Phu | 82 |
5 | 85 |Nova Daily | 86 |Lia Cover | 87 |
6 | 90 |Nova Daily | 91 |Joya Buss | 92 |
the output should be
95 |subscriber | 98 |
---|
Erwin Chitty | 101 |
Joya Buss | 104 |
Lavelle Phu | 107 |
Lia Cover | 110 |
Tonie Moreton | 113 |
source: codesignal.com 3 |
Your friend wants to become a professional tour guide and travel all around the world. In pursuit of this dream, she enrolled in tour guide school. The professors in this school turned out to be very demanding, and one of them gave your friend a difficult assignment that she has to finish over the weekend.
4 |Here's the task: Given a list of countries, your friend should calculate the average population and total population of all the countries in the list. To help her, you have decided to write a function that will calculate the required values for any number of countries. The countries table in which the countries are stored has the following structure:
5 |name
: the name of the country;continent
: the continent on which the country is situated;population
: the population of the country.Your task is to return a new table that contains the number of countries in the given list, along with their average and total population, in columns titled number
, average
and total
.
Example
12 |For the following table countries
13 |name | 16 |continent | 17 |population | 18 |
---|---|---|
Grenada | 21 |North America | 22 |103328 | 23 |
Monaco | 26 |Europe | 27 |38400 | 28 |
San Marino | 31 |Europe | 32 |33005 | 33 |
Vanuatu | 36 |Australia | 37 |277500 | 38 |
the output should be
41 |number | 44 |average | 45 |total | 46 |
---|---|---|
4 | 49 |113058.2500 | 50 |452233 | 51 |
source: codesignal.com 3 |
You recently started working in the IT department of a large store. You were put in charge of the inventory database availableItems, which has the following structure:
4 |id
: unique item ID;item_name
: the name of the item;item_type
: the type of the item.Note that it is possible for items that are of different types to have the same names.
10 |One of the most common operations performed on this database is querying the number of specific items available at the store. Since the database is quite large, queries of this type can take up too much time. You have decided to solve this problem by creating a new table that contains item counts for all available items.
11 |Given the availableItems table, compose a results table that has the following three columns: item_name
, item_type
and item_count
, containing the names of the items, their types, and the amount of those items, respectively. The table should be sorted in ascending order by item type, with items of the same type sorted in ascending order by their names.
Example
13 |For the following table availableItems
14 |id | 17 |item_name | 18 |item_type | 19 |
---|---|---|
1 | 22 |SafeDisk 4GB | 23 |USB drive | 24 |
2 | 27 |SafeDisk 8GB | 28 |USB drive | 29 |
3 | 32 |Cinco 50-Pack | 33 |DVD | 34 |
4 | 37 |SafeDisk 4GB | 38 |Memory card | 39 |
5 | 42 |SafeDisk 8GB | 43 |Memory card | 44 |
6 | 47 |Cinco 30-Pack | 48 |DVD | 49 |
7 | 52 |SafeDisk 4GB | 53 |Memory card | 54 |
8 | 57 |SafeDisk 4GB | 58 |Memory card | 59 |
9 | 62 |DiskHolder | 63 |Misc | 64 |
10 | 67 |Cinco 50-Pack | 68 |DVD | 69 |
11 | 72 |SafeDisk 4GB | 73 |USB drive | 74 |
12 | 77 |DiskCleaner Pro | 78 |Misc | 79 |
the output should be
82 |item_name | 85 |item_type | 86 |item_count | 87 |
---|---|---|
Cinco 30-Pack | 90 |DVD | 91 |1 | 92 |
Cinco 50-Pack | 95 |DVD | 96 |2 | 97 |
SafeDisk 4GB | 100 |Memory card | 101 |3 | 102 |
SafeDisk 8GB | 105 |Memory card | 106 |1 | 107 |
DiskCleaner Pro | 110 |Misc | 111 |1 | 112 |
DiskHolder | 115 |Misc | 116 |1 | 117 |
SafeDisk 4GB | 120 |USB drive | 121 |2 | 122 |
SafeDisk 8GB | 125 |USB drive | 126 |1 | 127 |
source: codesignal.com 3 |
You are curious about the geographical distribution of CodeSignal users, so you have created a list of countries along with the number of registered users from each. Your task now is to calculate the number of users on each continent.
4 |The information about the countries is stored in a table countries, which has 3
columns:
country
: the name of the country;continent
: the name of the continent where the country is located;users
: the number of users registered on CodeSignal in the country.The answer should be a table with 2
columns, continent
and users
, sorted by the number of users in decreasing order.
Example
12 |For the following table countries
13 |country | 16 |continent | 17 |users | 18 |
---|---|---|
Armenia | 21 |Europe | 22 |1000 | 23 |
France | 26 |Europe | 27 |1300 | 28 |
Russia | 31 |Europe | 32 |3000 | 33 |
USA | 36 |North America | 37 |5000 | 38 |
the resulting table should be
41 |continent | 44 |users | 45 |
---|---|
Europe | 48 |5300 | 49 |
North America | 52 |5000 | 53 |
source: codesignal.com 3 |
You want to expand your movie collection, but you don't really have any preferences so you're not sure where to start. After some consideration, you decide that you should start by finding more movies from award-winning directors whose movies you already own and who have shot a movie somewhat recently.
4 |To find the directors whose movies you might want to consider watching in the first place, you've created a database of all the films you already own and stored them in a moviesInfo table, which has the following structure:
5 |title
: the title of the movie;director
: the director of this movie;year
: the year the movie was released;oscars
: the number of the Academy Awards this movie received.Given the moviesInfo table, compose the list of directors you should consider watching more movies from. The resulting table should have a single director
column and contain the names of film directors such that:
2000
;2
.The table should be sorted by the directors' names in ascending order.
17 |Example
18 |For the following table moviesInfo
19 |title | 22 |director | 23 |year | 24 |oscars | 25 |
---|---|---|---|
BoBoiBoy: The Movie | 28 |Nizam Razak | 29 |2016 | 30 |0 | 31 |
Inception | 34 |Christopher Nolan | 35 |2010 | 36 |4 | 37 |
Interstellar | 40 |Christopher Nolan | 41 |2014 | 42 |1 | 43 |
Munna Bhai M.B.B.S. | 46 |Rajkumar Hirani | 47 |2003 | 48 |0 | 49 |
My Dear Brother | 52 |Ertem Egilmez | 53 |1973 | 54 |0 | 55 |
Rocky John | 58 |G. Avildsen | 59 |1976 | 60 |3 | 61 |
The Nights of Cabiria | 64 |Federico Fellini | 65 |1957 | 66 |1 | 67 |
The Sixth Sense | 70 |M. Night Shyamalan | 71 |1999 | 72 |6 | 73 |
The Sixth Sense | 76 |M. Night Shyamalan | 77 |1999 | 78 |6 | 79 |
Tokyo Story | 82 |Yasujirô Ozu | 83 |1953 | 84 |0 | 85 |
Yojimbo | 88 |Akira Kurosawa | 89 |1961 | 90 |1 | 91 |
the output should be
94 |director | 97 |
---|
Christopher Nolan | 100 |
source: codesignal.com 3 |
You are an avid traveler and you've visited so many countries that when people ask you where you've been, you can't even remember all of them! Luckily, every time you travel somewhere you write down the trip information in your diary. Now you want to get a list of all the different countries that you have visited using the information in your diary.
4 |The diary is represented as a table diary, which has the following columns:
5 |id
: the unique ID of the trip;travel_date
: the date the trip began;country
: the country to which you traveled.Given this diary table, create a semicolon-separated list of all the distinct countries you've visited, sorted lexicographically, and put the list in a table that has a single countries
column.
Example
12 |For the following table diary
13 |id | 16 |travel_date | 17 |country | 18 |
---|---|---|
1 | 21 |2008-05-12 | 22 |Ireland | 23 |
2 | 26 |2010-11-04 | 27 |France | 28 |
3 | 31 |2005-10-02 | 32 |Australia | 33 |
4 | 36 |2008-06-08 | 37 |Japan | 38 |
5 | 41 |2010-08-27 | 42 |Austria | 43 |
6 | 46 |2009-02-15 | 47 |France | 48 |
the output should be
51 |countries | 54 |
---|
Australia;Austria;France;Ireland;Japan | 57 |
source: codesignal.com 3 |
You have a table soccer_team that contains information about the players in your favorite soccer team. This table has the following structure:
4 |id
: the unique ID of the player;first_name
: the first name of the player;surname
: the last name of the player;player_number
: the number that the player wears (the number is guaranteed to be unique).Create a semicolon-separated list of all the players, sorted by their numbers, and put this list in a table under a column called players
. The information about each player should have the following format: first_name surname #number
.
Example
12 |For the following table soccer_team
13 |id | 16 |first_name | 17 |surname | 18 |player_number | 19 |
---|---|---|---|
1 22 | | Alexis | 23 |Sanchez | 24 |7 | 25 |
2 28 | | Petr | 29 |Cech | 30 |33 | 31 |
3 34 | | Hector | 35 |Bellerin | 36 |24 | 37 |
4 40 | | Olivier | 41 |Giroud | 42 |12 | 43 |
5 46 | | Theo | 47 |Walcott | 48 |14 | 49 |
6 52 | | Santi | 53 |Cazorla | 54 |19 | 55 |
the output should be
58 |players | 61 |
---|
Alexis Sanchez #7; Oliver Giroud #12; Theo Walcott #14; Santi Cazorla #19; Hector Bellerin #24; Petr Cech #33 | 64 |
source: codesignal.com 3 |
Your company is planning to expand internationally very soon. You have been tasked with preparing a report on foreign markets and potential competitors.
4 |After some investigation, you've created a database containing a foreignCompetitors table, which has the following structure:
5 |competitor
: the name of the competitor;country
: the country in which the competitor is operating.In your report, you need to include the number of competitors per country and an additional row at the bottom that contains a summary: ("Total:", total_number_of_competitors)
Given the foreignCompetitors table, compose the resulting table with two columns: country
and competitors
. The first column should contain the country name, and the second column should contain the number of competitors in this country. The table should be sorted by the country names in ascending order. In addition, it should have an extra row at the bottom with the summary, as described above.
Example
12 |For the following table foreignCompetitors
13 |competitor | 16 |country | 17 |
---|---|
Acme Corp | 20 |USA | 21 |
GLOBEX | 24 |USA | 25 |
Openmedia | 28 |France | 29 |
K-bam | 32 |USA | 33 |
Hatdrill | 36 |UK | 37 |
Hexgreen | 40 |Germany | 41 |
D-ranron | 44 |France | 45 |
Faxla | 48 |Spain | 49 |
the output should be
52 |country | 55 |competitors | 56 |
---|---|
France | 59 |2 | 60 |
Germany | 63 |1 | 64 |
Spain | 67 |1 | 68 |
UK | 71 |1 | 72 |
USA | 75 |3 | 76 |
Total: | 79 |8 | 80 |
source: codesignal.com 3 |
You've been dreaming about becoming a famous hacker all your life, and now it's time to make your dreams come true! You decided to start by finding a website that has some vulnerability, and you just found a doozy. This particular website has an open database users
that contains information about the people using it. What's more, it stores the queries performed on this table on the client side, which makes it super simple to hack them.
The users table contains the following columns:
6 |id
- The unique user's ID;login
- The unique user's login;name
- The user's name;type
- The user's role type (which can be "user"
, "admin"
, "moderator"
, etc.).The query you have access to gathers some information about the users who have the "user"
type
. You don't want to get caught, so you want to carefully update it so that the query will return the records of all existing type
s.
Your task is to update the existing query. Note: You should add something to the query, but don't rewrite it.
14 |Example
15 |For the given table users
16 |id | 19 |login | 20 |name | 21 |type | 22 |
---|---|---|---|
1 | 25 |Ann92 | 26 |Ann | 27 |user | 28 |
2 | 31 |Bob88 | 32 |Bob | 33 |user | 34 |
3 | 37 |Carol412 | 38 |Carol | 39 |admin | 40 |
4 | 43 |Deril39 | 44 |Deril | 45 |admin | 46 |
5 | 49 |EvaGrin | 50 |Eva | 51 |user | 52 |
the existing query returns the following table:
55 |id | 58 |login | 59 |name | 60 |
---|---|---|
1 | 63 |Ann92 | 64 |Ann | 65 |
2 | 68 |Bob88 | 69 |Bob | 70 |
5 | 73 |EvaGrin | 74 |Eva | 75 |
but the updated query should return the following one:
78 |id | 81 |login | 82 |name | 83 |
---|---|---|
1 | 86 |Ann92 | 87 |Ann | 88 |
2 | 91 |Bob88 | 92 |Bob | 93 |
3 | 96 |Carol412 | 97 |Carol | 98 |
4 | 101 |Deril39 | 102 |Deril | 103 |
5 | 106 |EvaGrin | 107 |Eva | 108 |
source: codesignal.com 3 |
Your company hired an intern database engineer, who immediately started updating the data in the system. Unfortunately, he hasn't fully grasped the concept of NULL
values yet and he performed some incorrect inserts and updates to the departments table, which has the following structure:
id
: the unique department ID;name
: the name of the department;description
: the description of the department.Now you have a table where the description
column holds values such as ' NULL '
, NULL
, 'nil'
and ' - '
. You need to find out exactly how many records in the table should have NULL
in the description
column, regardless of whether the intern input the value correctly or not.
He used the following values to indicate NULL
:
NULL
: just a regular NULL value;'<spaces>NULL<spaces>'
: NULL as a case insensitive (i.e. NuLl
is also OK) string with an arbitrary number of spaces at the beginning and at the end;'<spaces>nil<spaces>'
: nil as a case insensitive (i.e. niL
is also OK) string with an arbitrary number of spaces at the beginning and at the end;'<spaces>-<spaces>'
: a single dash with an arbitrary number of spaces at the beginning and at the end.Given the departments table, compose the resulting table with the single column number_of_nulls
containing a single value: the number of rows in the departments table that are supposed to have NULL
in the description
.
Example
19 |For the following table departments
20 |id | 23 |name | 24 |description | 25 |
---|---|---|
1 | 28 |finance | 29 |NULL | 30 |
2 | 33 |marketing | 34 |' NULL' | 35 |
3 | 38 |IT | 39 |'not nil' | 40 |
4 | 43 |accounting | 44 |'-' | 45 |
the output should be
48 |number_of_nulls | 51 |
---|
3 | 54 |
source: codesignal.com 3 |
You really love counting stuff. In fact, right now you're sitting on a meadow and counting all the legs you see there! There are some people, some dogs, and some cats on the meadow. You're pretty confident that each human has 2
legs and that cats and dogs have 4
legs each.
The information about all the creatures on the meadow is stored in the table creatures. It has the following structure:
5 |id
: the unique creature ID;name
: the creature's name;type
: the creature's type - "human"
, "cat"
, or "dog"
. This column has ENUM
type.You want to calculate the total number of legs in the meadow. Given the table creatures, build a new table that only contains one column summary_legs
and has only one row with the total number of legs that you can see.
Example
12 |For the following table creatures
13 |id | 16 |name | 17 |type | 18 |
---|---|---|
1 | 21 |Mike | 22 |human | 23 |
2 | 26 |Misty | 27 |cat | 28 |
3 | 31 |Max | 32 |dog | 33 |
4 | 36 |Tiger | 37 |human | 38 |
the output should be
41 |summary_legs | 44 |
---|
12 | 47 |
There are 2
humans, 1
cat, and 1
dog, so there are 2 * 2 + 1 * 4 + 1 * 4 = 12
legs in total.
source: codesignal.com 3 |
You returned from vacation with a huge suitcase full of presents. Unfortunately, you forgot the correct combination for the combination lock on the bag, and now you have to try all of them until you find the correct one. You're curious about how many possible combinations the lock has.
4 |The combination lock consists of several rotating discs, where each disc has its own set of possible characters. You have a table discs which stores the information about these discs and has the following structure:
5 |id
: the unique ID of a disc;characters
: the list of characters the disc has (the characters are guaranteed to be unique);color
: the color of the disc.Calculate the total number of all possible combinations that the lock has, and return it as a table that has only one column combinations
and one row.
Example
12 |For the following table discs
13 |id | 16 |characters | 17 |color | 18 |
---|---|---|
1 | 21 |code | 22 |blue | 23 |
2 | 26 |fights | 27 |white | 28 |
the output should be
31 |combinations | 34 |
---|
24 | 37 |
The set of possible characters for the first disc is equal to 4
, and the set for the second disc is 6
, so the total number of combinations is 4 * 6 = 24
.
source: codesignal.com 3 |
CREATE PROCEDURE interestClub()
5 | SELECT name
6 | FROM people_interests
7 | WHERE interests & ... AND interests & ...
8 | ORDER BY name
9 | You are the chairman of your university's drawing club, which isn't doing very well right now. The club meets two times a week to exchange drawing advice, talk about new techniques, and draw something together. But the members are starting to get bored during these meetings, so you've decided to add an additional activity to the routine.
11 |In order to do this, you decided to collect information about the students, which is now stored in the table people_interests, which has the following columns:
12 |name
- the unique name of a person;interests
- the set of interests or hobbies this person has, given as a comma-joined string. This column has datatype set('reading','sports','swimming','drawing','writing','acting','cooking','dancing','fishkeeping','juggling','sculpting','videogaming')
.This information gave you the idea that reading might be an interesting theme for the next meeting, so you announced that the next meeting will be reading-related. Now you're interested in the number of members that will come.
17 |Given the people_interests table, find the people who will attend the next meeting, i.e. those who are fond of both drawing and reading. The resulting table should consist of a single name
column, and the records should be sorted by people's names.
Example
19 |For given table people_interests
20 |name | 23 |interests | 24 |
---|---|
August | 27 |cooking,juggling | 28 |
Buddy | 31 |reading,swimming,drawing,acting,dancing,videogaming | 32 |
David | 35 |juggling,sculpting | 36 |
Dennis | 39 |swimming,cooking,fishkeeping | 40 |
James | 43 |reading,drawing | 44 |
the output should be
47 |name | 50 |
---|
Buddy | 53 |
James | 56 |
source: codesignal.com 3 |
You've been looking for new friends on the Internet. You just came across a cool website that helps people all over the world become friends by suggesting perfect friend candidates based on the information in users' profiles.
4 |The system suggested some people for you to reach out to, and you're ready to make a move. However, you don't want to exercise your communication skills in vain, which is why prior to contacting a person you want to make sure you'll have something in common to talk about. The best option is to check whether they have the same hobbies you do, which are sports and reading.
5 |You downloaded the list of suggested people and saved it in the people_hobbies table, which has the following structure:
6 |name
- the unique person's name;hobbies
- a list of hobbies the person has (this column is of the datatype set
).Given the people_hobbies table, your goal is to return the sorted names of people who have sports and reading in their list of hobbies
.
Example
12 |For the given table people_hobbies
13 |name | 16 |hobbies | 17 |
---|---|
Adam | 20 |swimming | 21 |
Amy | 24 |reading,sports,swimming | 25 |
Carl | 28 |filmwatching,writing | 29 |
Carol | 32 |reading,swimming | 33 |
Deril | 36 |sports | 37 |
Jake | 40 |reading,sports,swimming | 41 |
Lola | 44 |reading,sports,painting | 45 |
Nina | 48 |sports,painting | 49 |
Sam | 52 |sports | 53 |
the output should be
56 |name | 59 |
---|
Amy | 62 |
Lola | 65 |
As you can see, only Amy and Lola have both reading and sports in their hobbies
lists.
source: codesignal.com 3 |
You have your very own library at home, and it's getting bigger and bigger with each passing month. You've decided to create a database in which to store information about your books, in the hope that it will help you remember which books you have in your library.
4 |Information about the books in your library is stored in the table catalogs, which contains the following columns:
5 |doc_id
- the unique ID of the catalog;xml_doc
- the catalog as an XML file in the following format:<catalog>
8 | <book id="...">
9 | <author>...</author>
10 | <title>...</title>
11 | </book>
12 | <book id="...">
13 | <author>...</author>
14 | <title>...</title>
15 | </book>
16 | ...
17 | </catalog>.
18 |
19 | Each catalog represents the work of one distinct <author>
in your library. There is exactly one <catalog>
element in each xml_doc
, and the id
for each book is unique.
Given the catalogs table, you want to find out which authors you have represented in your library. Your task is to create a new table with the author
column that will contain all the distinct authors, sorted by their names.
Example
24 |For given table catalogs
25 |doc_id | 28 |xml_doc | 29 |
---|---|
1 | 32 |<catalog> 33 | <book id="11"> 34 | <author>Chuck Palahniuk</author> 35 | <title>Fight Club</title> 36 | </book> 37 | <book id="12"> 38 | <author>Chuck Palahniuk</author> 39 | <title>Survivor</title> 40 | </book> 41 | </catalog> |
42 |
2 | 45 |<catalog> 46 | <book id="21"> 47 | <author>Bernard Werber</author> 48 | <title>Les Thanatonautes</title> 49 | </book> 50 | </catalog> |
51 |
3 | 54 |<catalog> 55 | <book id="31"> 56 | <author>Boris Vian</author> 57 | <title>The Big Sleep</title> 58 | </book> 59 | <book id="32"> 60 | <author>Boris Vian</author> 61 | <title>The Lady in the Lake</title> 62 | </book> 63 | <book id="33"> 64 | <author>Boris Vian</author> 65 | <title>The World of Null-A</title> 66 | </book> 67 | </catalog> |
68 |
the output should be
71 |author | 74 |
---|
Bernard Werber | 77 |
Boris Vian | 80 |
Chuck Palahniuk | 83 |
source: codesignal.com 3 |
As a young naturalist, you've been studying the inhabitants of the nearby woods for the past several months. You've just come across some footprints you've never seen before. To learn more about the habitat of the animal that left them, you marked the footprints locations on your map.
4 |The information about the places where the animal left its footprints is stored in the table places. Here is its structure:
5 |x
: the x
-coordinate of the place;y
: the y
-coordinate of the place.It is guaranteed that pairs (x, y)
are unique.
Now you want to find the area of the animal's habitat. You decided that the convex hull of the marked points is a good first approximation of the habitat, so you want to find the area of this hull.
11 |Given the places table, return a table that has only one column area
and consists of a single row: the area of the convex hull. It is guaranteed that the resulting area is greater than 0
.
Example
13 |For the following table places
14 |x | 17 |y | 18 |
---|---|
0 | 21 |0 | 22 |
1 | 25 |2 | 26 |
2 | 29 |1 | 30 |
5 | 33 |1 | 34 |
5 | 37 |2 | 38 |
the output should be
41 |area | 44 |
---|
6.5 | 47 |
Here is an illustration of the given points and their convex hull:
50 | 51 |Note that you should return the exact answer without any trailing zeros.
52 |source: codesignal.com 3 |
The kingdom has been given terrible news: the King has passed away. While the nation is mourning, the noblemen need to decide who will take the throne next.
4 |The late King had many children, and now it is necessary to determine their order of succession according to their seniority.
5 |The list of the King's children is represented as a table Successors with the following attributes:
6 |name
: the child's name;birthday
: the date of their birthday (it is guaranteed that birthday dates are unique);gender
: their gender (a character equal to 'M'
or 'F'
).The resulting table should contain the names of the King's heirs in order of their succession to the throne as determined by their age, and preceded by their potential future titles (i.e. "King name"
or "Queen name"
).
Example
13 |For the following table Successors
14 |name | 17 |birthday | 18 |gender | 19 |
---|---|---|
Amelia | 22 |1711-06-10 | 23 |F | 24 |
Anne | 27 |1709-11-02 | 28 |F | 29 |
Caroline | 32 |1713-06-10 | 33 |F | 34 |
Frederick | 37 |1707-02-01 | 38 |M | 39 |
Loisa | 42 |1724-12-18 | 43 |F | 44 |
Mary | 47 |1723-03-05 | 48 |F | 49 |
William | 52 |1721-04-26 | 53 |M | 54 |
The output should be
57 |name | 60 |
---|
King Frederick | 63 |
Queen Anne | 66 |
Queen Amelia | 69 |
Queen Caroline | 72 |
King William | 75 |
Queen Mary | 78 |
Queen Loisa | 81 |
source: codesignal.com 3 |
You've started to receive a lot of emails every day, and you decide to sort them in order to make it quicker to navigate through them.
4 |Information about your emails is stored in a table emails, which has the following structure:
5 |id
: the unique email id;email_title
: the title of the email;size
: the size of the email's body in bytes.You decide to sort all the emails by their body sizes in descending order, because you think that the bigger an email is the more important it is. However, you don't like having the sizes written in bytes because they are usually too large and don't make much sense. You want them to be written in kilobytes (1 Kb = 210 bytes
) and megabytes (1 Mb = 220 bytes
) instead, rounded down if necessary. For example, 21432432
bytes is equal to 20
megabytes and 460912
bytes, so the result should be rounded down to 20 Mb
.
Given the table emails, build a table as follows: The resulting table should have the columns id
, email_title
, and short_size
, and should be sorted in descending order by the initial sizes of the emails. It is guaranteed that all the emails are of unique sizes, so there will not be any ties.
Example
13 |For the following table emails
14 |id | 17 |email_title | 18 |size | 19 |
---|---|---|
1 | 22 |You won 1M dollars! | 23 |21432432 | 24 |
2 | 27 |You are fired | 28 |312342 | 29 |
3 | 32 |Black Friday is coming | 33 |323 | 34 |
4 | 37 |Spam email | 38 |23532 | 39 |
5 | 42 |Your requested backup | 43 |234234324 | 44 |
the output should be
47 |id | 50 |email_title | 51 |short_size | 52 |
---|---|---|
5 | 55 |Your requested backup | 56 |223 Mb | 57 |
1 | 60 |You won 1M dollars! | 61 |20 Mb | 62 |
2 | 65 |You are fired | 66 |305 Kb | 67 |
4 | 70 |Spam email | 71 |22 Kb | 72 |
3 | 75 |Black Friday is coming | 76 |0 Kb | 77 |
source: codesignal.com 3 |
You are trying to decide where you want to go on your vacation, so your travel agency has provided you with a database of possible destinations.
4 |This database contains the table countryActivities, which has the following structure:
5 |id
: the unique id of the record;country
: the country name;region
: the region of this country;leisure_activity_type
: the type of activity provided in the region. This can only be equal to one of the following values: Adventure park
, Golf
, Kart racing
, River cruise
;number_of_places
: the number of resorts in the region at which you can do this activity.You want to see how many resorts in each country provide the activities you are interested in before you decide where to go on your vacation, but it's hard to do this using only the table provided by your travel agency. To make things easier, you have decided to create a new table with a better structure.
13 |Given the countryActivities table, compose the resulting table with five columns: country
, adventure_park
, golf
, river_cruise
and kart_racing
. The first column should contain the country name, while the second, third, fourth, and fifth columns should contain the number of resorts in the country that offer Adventure park
, Golf
, River cruise
, and Kart racing
, respectively. The table should be sorted by the country names in ascending order.
Example
15 |For the following table countryActivities
16 |id | 19 |country | 20 |region | 21 |leisure_activity_type | 22 |number_of_places | 23 |
---|---|---|---|---|
1 | 26 |France | 27 |Normandy | 28 |River cruise | 29 |2 | 30 |
2 | 33 |Germany | 34 |Bavaria | 35 |Golf | 36 |5 | 37 |
3 | 40 |Germany | 41 |Berlin | 42 |Adventure park | 43 |2 | 44 |
4 | 47 |France | 48 |Ile-de-France | 49 |River cruise | 50 |1 | 51 |
5 | 54 |Sweden | 55 |Stockholm | 56 |River cruise | 57 |3 | 58 |
6 | 61 |France | 62 |Normandy | 63 |Kart racing | 64 |4 | 65 |
the output should be
68 |country | 71 |adventure_park | 72 |golf | 73 |river_cruise | 74 |kart_racing | 75 |
---|---|---|---|---|
France | 78 |0 | 79 |0 | 80 |3 | 81 |4 | 82 |
Germany | 85 |2 | 86 |5 | 87 |0 | 88 |0 | 89 |
Sweden | 92 |0 | 93 |0 | 94 |3 | 95 |0 | 96 |
source: codesignal.com 3 |
You have a table scores that contains information about a series of soccer games. Your goal is to determine the winner of the series. A team is declared the winner if it won more games than the other team. If both teams had the same number of wins, then the winner is determined by the better goal difference (the difference between the goals that a team scores and the goals that the opposing team scores on them over all the games). If the goal differences are equal, the winner is the team that scored more goals during away games (i.e. games when it was not the host team). The result is the index of the winning team. If the above criteria are not sufficient for determining the winner, return 0
.
The scores table contains the following columns:
5 |match_id
- the unique ID of the match;first_team_score
- the score of the 1st
team in the current match;second_team_score
- the score of the 2nd
team in the current match;match_host
- the team that is the host of the match (can be 1
or 2
).Your task is to return a new table with a single column winner
, which can contain 1
, 2
, or 0
.
Example
13 |For given table scores
14 |match_id | 17 |first_team_score | 18 |second_team_score | 19 |match_host | 20 |
---|---|---|---|
1 | 23 |3 | 24 |2 | 25 |1 | 26 |
2 | 29 |2 | 30 |1 | 31 |2 | 32 |
3 | 35 |1 | 36 |2 | 37 |1 | 38 |
4 | 41 |2 | 42 |1 | 43 |2 | 44 |
the output should be
47 |winner | 50 |
---|
1 | 53 |
The first team won 3
games out of 4
, so it's the winner of the series.
source: codesignal.com 3 |
The company you work for has a database with the IP addresses of all the company's computers in it. When a new computer is purchased, an employee adds its IPv4 address to this database. Unfortunately, you've just discovered that there is no address validation, so some of the records are incorrect.
4 |Now your boss wants you to write a program that will find and retain only the correct records from this table. A record is correct if the IP it contains is a valid IPv4 address, and either the first or the second component in the host part is a two-digit number.
5 |The ips table contains the following columns:
6 |id
: the unique ID of the computer;ip
: the unique IP address of the computer.Your task is to create a new table that contains only the correct records from the given ips table. This table should be sorted by id
.
Example
12 |For the given table ips
13 |id | 16 |ip | 17 |
---|---|
4 | 20 |"1.1.1.1" | 21 |
3 | 24 |"1.111.111.11" | 25 |
2 | 28 |"11.11.11.11" | 29 |
1 | 32 |"11.11.11.11.11.11" | 33 |
5 | 36 |"11.11.11.111" | 37 |
the output should be
40 |id | 43 |ip | 44 |
---|---|
2 | 47 |"11.11.11.11" | 48 |
3 | 51 |"1.111.111.11" | 52 |
5 | 55 |"11.11.11.111" | 56 |
source: codesignal.com 3 |
You recently started a new job, and you were frustrated to discover that the company you joined has a very poorly written front-end client application. It doesn't even have validation checks for data entered by users! The biggest problem is with the phone_number
field, which your company uses to send automatic notifications to users.
Your first task as a company employee is to retrieve all the valid records from the given table of phone numbers. The record is valid if its phone_number
is a valid American or Canadian phone number, which means that it has a format of 1-###-###-####
or (1)###-###-####
, where #
stands for any digit from 0
to 9
.
Information is stored in the table phone_numbers, which has the following fields:
6 |name
: the user's first name;surname
: the user's unique surname;phone_number
: the user's unique phone number.You should return a new table that contains only the valid records from the phone_numbers table, sorted by the users' surnames.
12 |Example
13 |For the following table phone_numbers
14 |name | 17 |surname | 18 |phone_number | 19 |
---|---|---|
Cornelius | 22 |Walsh | 23 |1-234-567-8910 | 24 |
Frank | 27 |McKenzie | 28 |1-2345-678-911 | 29 |
John | 32 |Smith | 33 |(1)111-111-1111 | 34 |
Lester | 37 |Goodwin | 38 |(1)-111-111-1111 | 39 |
the output should be
42 |name | 45 |surname | 46 |phone_number | 47 |
---|---|---|
John | 50 |Smith | 51 |(1)111-111-1111 | 52 |
Cornelius | 55 |Walsh | 56 |1-234-567-8910 | 57 |
source: codesignal.com 3 |
You're very busy and have a lot of important events coming up. In order to ensure that you don't forget any of these events, you have decided to organize them.
4 |The information about your events is stored in the table events, which has the structure:
5 |id
: unique event id;name
: the event name;event_date
: the event date in the format YYYY-MM-DD
;participants
: the number of people that are going to attend this event.After some thinking, you decide that it would be easier to navigate though your schedule if you could see all the events ordered by the weekday on which they are scheduled. In order to do so, you want to sort all the events by the weekdays of their event_date
s, meaning that Monday events should come first, Tuesday events should come next, and so on, with Sunday events coming last. In the case of a tie, the participants
should be a tie-breaker; an event with the largest number of participants
should go first, because events with more attendees are more important. It is guaranteed that there are no events that have the same event_date
and the same number of participants
.
Given the table events, sort it as described above and return the resulting table.
13 |Example
14 |For the following table events
15 |id | 18 |name | 19 |event_date | 20 |participants | 21 |
---|---|---|---|
1 | 24 |Dinner | 25 |2016-11-27 | 26 |3 | 27 |
2 | 30 |Comic-con | 31 |2016-10-25 | 32 |100 | 33 |
3 | 36 |Christmas | 37 |2016-12-31 | 38 |5000 | 39 |
4 | 42 |Meeting | 43 |2016-10-18 | 44 |300 | 45 |
the output should be
48 |id | 51 |name | 52 |event_date | 53 |participants | 54 |
---|---|---|---|
4 | 57 |Meeting | 58 |2016-10-18 | 59 |300 | 60 |
2 | 63 |Comic-con | 64 |2016-10-25 | 65 |100 | 66 |
3 | 69 |Christmas | 70 |2016-12-31 | 71 |5000 | 72 |
1 | 75 |Dinner | 76 |2016-11-27 | 77 |3 | 78 |
Meeting
and Comic-con
are both scheduled for Tuesdays, but Meeting
is more important because its participants
number is bigger. Christmas
is scheduled for Saturday and Dinner
is scheduled for Sunday.
source: codesignal.com 3 |
Your company's accounting department hasn't been doing great work lately, and they've been very sloppy when entering information into their database.
4 |The information about each accounting document is stored in a documents table with 2
columns:
id
: the unique id of the document;date
: the date the document was created, as a string in the format YYYY-MM-DD
(of type VARCHAR(10)
). Since input has been sloppy, the leading zeros of days and months have sometimes been omitted.The omission of these leading zeros for days and months in the date
column is making operations on the database prone to errors. To minimize the number of errors without changing the table structure, you need to translate all the dates to ISO format YYYY-MM-DD
.
Given the documents table, return a table with one column date_iso
that contains all the dates from documents sorted by the ids of the corresponding documents.
Example
12 |For the following table documents
13 |id | 16 |date_str | 17 |
---|---|
1 | 20 |2000-1-1 | 21 |
2 | 24 |2014-8-21 | 25 |
3 | 28 |2002-03-07 | 29 |
4 | 32 |2008-10-5 | 33 |
5 | 36 |2016-12-17 | 37 |
the output should be
40 |date_iso | 43 |
---|
2000-01-01 | 46 |
2014-08-21 | 49 |
2002-03-07 | 52 |
2008-10-05 | 55 |
2016-12-17 | 58 |
source: codesignal.com 3 |
During the most recent social event you attended, you suddenly realized that you forgot your USB drive at a previous event. You're pretty sure that you had your flash drive with you just last week, which means that you probably lost it during one of the events of the last 7
days. You want to find all the events you attended during this period.
The list of events you've attended (including the most recent one) is stored in a table called Events. It has three columns:
5 |id
: the unique id of the event;name
: the name of the event;event_date
: the date of the event.You want to come up with the list of all the events you attended over the past 7
days, except for the very last one (since you know you lost your flash drive before then). Return this list as a table with columns name
and event_date
sorted by event dates in descending order.
It is guaranteed that there is at most one event on any given day.
12 |Example
13 |For the following table Events
14 |id | 17 |name | 18 |event_date | 19 |
---|---|---|
1 | 22 |TGIF | 23 |2016-11-18 | 24 |
2 | 27 |TGIF | 28 |2016-11-11 | 29 |
3 | 31 |Weekly team meeting | 32 |2016-11-07 | 33 |
4 | 36 |Weekly team meeting | 37 |2016-11-14 | 38 |
the output should be
41 |name | 44 |event_date | 45 |
---|---|
Weekly team meeting | 47 |2016-11-14 | 48 |
TGIF | 50 |2016-11-11 | 51 |
source: codesignal.com 3 |
You own a small company, and you keep track of its income in the accounting table, which has the following structure:
4 |date
: a unique date on which your company was open;profit
: the amount of money your company earned that day;loss
: the amount of money your company lost that day.You've decided to sell the company, and in order to make the offer more appealing to potential buyers you need to create a financial report.
10 |Given the accounting table, compose the resulting table with three columns: year
, quarter
and net_profit
. The first column should contain the year, the second one should contain the quarter of that year, and the third one should contain the net income (profit - loss
difference) of your company during that period. The resulting table should be sorted by the year
in ascending order. If there are several rows with the same year
, sort them by the quarter
in ascending order.
Don't include year/quarter in the answer if there is no entry for it in the accounting table.
12 |Example
13 |For the following table accounting
14 |date | 17 |profit | 18 |loss | 19 |
---|---|---|
2006-01-01 | 22 |100 | 23 |15 | 24 |
2006-07-15 | 27 |40 | 28 |100 | 29 |
2006-08-01 | 32 |50 | 33 |50 | 34 |
2006-11-11 | 37 |100 | 38 |50 | 39 |
2006-12-01 | 42 |50 | 43 |80 | 44 |
2007-05-03 | 47 |42 | 48 |16 | 49 |
the output should be
52 |year | 55 |quarter | 56 |net_profit | 57 |
---|---|---|
2006 | 60 |1 | 61 |85 | 62 |
2006 | 65 |3 | 66 |-60 | 67 |
2006 | 70 |4 | 71 |20 | 72 |
2007 | 75 |2 | 76 |26 | 77 |
source: codesignal.com 3 |
You are developing an alarm clock app that works as follows: the user can set a date and a time, and the app will ring every week at the given time, starting at the given date until the end of the current year.
4 |The starting date is the only record in the userInput table, which has the following structure:
5 |input_date
: the date and time of the first alarm (of a DATETIME
type).Given the table, your task is to compose the resulting table with a single column alarm_date
. This column should contain all dates (including the time) when the alarm clock will ring. The entries should be arranged in ascending chronological order.
Example
10 |For the following table userInput
11 |input_date | 14 |
---|
2016-10-23 22:00:00 | 17 |
the output should be
20 |alarm_date | 23 |
---|
2016-10-23 22:00:00 | 25 |
2016-10-30 22:00:00 | 28 |
2016-11-06 22:00:00 | 31 |
2016-11-13 22:00:00 | 34 |
2016-11-20 22:00:00 | 37 |
2016-11-27 22:00:00 | 40 |
2016-12-04 22:00:00 | 43 |
2016-12-11 22:00:00 | 46 |
2016-12-18 22:00:00 | 49 |
2016-12-25 22:00:00 | 52 |
source: codesignal.com 3 |
You work at a small startup company that only has a handful of employees. Since the company is so small, some of the employees have to work in several departments at once. Because of this, information about groups of employees that work in the same departments is stored in two separate tables, departments and employees. The tables are structured as follows:
4 |dep_name
: the unique department name;emp_name
: the employee's unique first name and surname.Given the tables departments and employees, you have to prepare a report about company employees and the departments in which they work. To do this, you simply combine the rows of the two tables, then sort the resulting rows first by the departments' names and then by the employees' names.
17 |Example
18 |For the following tables departments
19 |dep_name | 22 |
---|
IT | 25 |
PD | 28 |
and employees:
31 |emp_name | 34 |
---|
Donald Cole | 37 |
James Richard | 40 |
John Tucker | 43 |
the output should be
46 |dep_name | 49 |emp_name | 50 |
---|---|
IT | 53 |Donald Cole | 54 |
IT | 57 |James Richard | 58 |
IT | 61 |John Tucker | 62 |
PD | 65 |Donald Cole | 66 |
PD | 69 |James Richard | 70 |
PD | 73 |John Tucker | 74 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /44-scholarshipsDistribution/scholarshipsDistribution.sql: -------------------------------------------------------------------------------- 1 | CREATE PROCEDURE scholarshipsDistribution() 2 | BEGIN 3 | SELECT candidates.candidate_id as student_id 4 | FROM candidates 5 | LEFT JOIN detentions 6 | ON candidates.candidate_id = detentions.student_id 7 | WHERE detentions.student_id IS NULL; 8 | END 9 | -------------------------------------------------------------------------------- /45-userCountries/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 |
You are really interested in statistics, and your new project is to gather some information about the users of a big social network. More specifically, you want to know which countries these users are from. Using the social network's API, you managed to collect enough data to compose two tables users and cities, which have the following structures:
4 |id
: the unique user ID;city
: the name of the city where this user lives;city
: a unique city name;country
: the name of the country where this city is located.Given the tables users and cities, build the resulting table so that it consists of the columns id
and country
. The table should consist of user id
s, and for each user the country where they live should be given in the country
column. If a user's city is missing from the cities table, the country
column should contain "unknown"
instead.
Return the table sorted by users' id
s.
Example
21 |For the following table users
22 |id | 25 |city | 26 |
---|---|
1 | 29 |San Francisco | 30 |
2 | 33 |Moscow | 34 |
3 | 37 |London | 38 |
4 | 41 |Washington | 42 |
5 | 45 |New York | 46 |
6 | 49 |Saint Petersburg | 50 |
7 | 53 |Helsinki | 54 |
and the following table cities
57 |city | 60 |country | 61 |
---|---|
Moscow | 64 |Russia | 65 |
Saint Petersburg | 68 |Russia | 69 |
San Francisco | 72 |USA | 73 |
Washington | 76 |USA | 77 |
New York | 80 |USA | 81 |
London | 84 |England | 85 |
the output should be
88 |id | 91 |country | 92 |
---|---|
1 | 95 |USA | 96 |
2 | 99 |Russia | 100 |
3 | 103 |England | 104 |
4 | 107 |USA | 108 |
5 | 111 |USA | 112 |
6 | 115 |Russia | 116 |
7 | 119 |unknown | 120 |
source: codesignal.com 3 |
You're going on vacation in a big city, and you've prepared a list of all the sights you'd like to visit. As you don't have much time and can't spend a whole day at one interesting place, you want to visit several sights per day and walk from one site to another. But walking a long distance might be boring, so you won't walk between two places if the distance between them is 5
km or more.
To find the route that you should take, you are going to find all pairs of places that are less than 5
km from each other.
The places of interests are stored in a table sights, which has the following attributes:
6 |id
: the unique ID of the place;name
: the name of the place;x
: the x coordinate of the place;y
: the y coordinate of the place.The distance between the places is calculated with the assumption that they are just points on a 2D map.
13 |Given the sights table, find all pairs of places that are less than 5
km from each other and return them as a table with the columns place1
and place2
. Sort them in lexicographical order. The places in the pairs should also be sorted lexicographically.
Example
15 |For the following table sights
16 |id | 19 |name | 20 |x | 21 |y | 22 |
---|---|---|---|
1 | 25 |Tower of London | 26 |51508.026 | 27 |-7.5939 | 28 |
2 | 31 |Trafalgar Square | 32 |51508.040 | 33 |-12.7899 | 34 |
3 | 37 |London Eye | 38 |51503.538 | 39 |-11.9371 | 40 |
4 | 43 |The Shard | 44 |51504.533 | 45 |-8.6028 | 46 |
the output should be
49 |place1 | 52 |place2 | 53 |
---|---|
London Eye | 56 |The Shard | 57 |
London Eye | 60 |Trafalgar Square | 61 |
The Shard | 64 |Tower of London | 65 |
https://codesignal.s3.amazonaws.com/tasks/placesOfInterestPairs/img/ex.png?_tm=1551433043558"
68 |source: codesignal.com 3 |
You are developing a calendar application that will allow users to schedule various events on different dates. The dates are all stored in the server local time as a DATETIME
. You want to display the dates using the settings specified by the user.
Event dates and user settings are stored in the events and settings tables, which have the following structures:
5 |event_id
: the unique event ID;date
: the time and date of the event;user_id
: the ID of the user who scheduled this event.user_id
: the unique ID of the user with these settings;timeshift
: the number of minutes the user's time differs from the server (this could potentially be negative or zero);hours
: the number of hours in the user's clock (either 12
or 24
).The displayed dates should have the format yyyy-mm-dd hh:mm
if the user's clock has 24 hours. Otherwise, the format should be yyyy-mm-dd hh:mm <p>
, where <p>
is either AM
or PM
depending on the time.
Given the events and settings tables, compose the result as follows: The resulting table should have two columns, event_id
and formatted_date
, that contain the event IDs and the formatted event dates according to settings of the user who scheduled that event, respectively. The table should be sorted in ascending order by the event IDs.
Example
24 |The following tables events
25 |event_id | 28 |date | 29 |user_id | 30 |
---|---|---|
1 | 33 |2011-11-11 13:37:00 | 34 |4242 | 35 |
2 | 38 |2012-12-12 04:04:00 | 39 |4987 | 40 |
3 | 43 |2016-01-01 11:19:00 | 44 |4242 | 45 |
4 | 48 |2011-11-11 13:37:00 | 49 |4987 | 50 |
and settings
53 |user_id | 56 |timeshift | 57 |hours | 58 |
---|---|---|
1 | 61 |30 | 62 |24 | 63 |
4242 | 66 |0 | 67 |24 | 68 |
4987 | 71 |60 | 72 |12 | 73 |
the output should be
76 |event_id | 79 |formatted_date | 80 |
---|---|
1 | 83 |2011-11-11 13:37 | 84 |
2 | 87 |2012-12-12 05:04 AM | 88 |
3 | 91 |2016-01-01 11:19 | 92 |
4 | 95 |2011-11-11 02:37 PM | 96 |
source: codesignal.com 3 |
You've been traveling all over the world for the past year, and now it's time for you to come home. You marked each city that you visited on a map in the order that you visited them, and wrote down the cities' coordinates. For the sake of simplicity, the cities are assigned coordinates on a plane map, so they can be written as (x, y)
.
The information about the cities you visited is stored in the table cities, which has the structure:
5 |id
: the unique city ID;x
: the x coordinate of the city;y
: the y coordinate of the city.Now that your journey is over, you're curious about the distance you covered. Given the cities table, your task is to calculate the total length of your route as follows: First, calculate the Euclidean distance between the first and second cities, then add it to the distance between the second and the third cities, and so on.
11 |Return the table with a single total
column containing only one row which contains the total length of your route, calculated as described above.
Example
13 |For the following table cities
14 |id | 17 |x | 18 |y | 19 |
---|---|---|
1 | 22 |0 | 23 |0 | 24 |
2 | 27 |1 | 28 |1 | 29 |
3 | 32 |2 | 33 |2 | 34 |
4 | 37 |4 | 38 |2 | 39 |
the output should be
42 |total | 45 |
---|
4.828427125 | 48 |
Here is how the answer was calculated:
51 | distance(0, 0, 1, 1) + distance(1, 1, 2, 2) + distance(2, 2, 4, 2) = sqrt(2) + sqrt(2) + 2 = 4.828427125
.
The answer should be rounded to exactly 9
digits after the decimal point.
source: codesignal.com 3 |
You are developing an application for working with different currencies. You've decided to represent each currency using three letter codes from ISO 4217.
4 |You've parsed some data that you found on the Internet and stored it in a currencies table, which has the following structure:
5 |code
: the unique code of the currency;country
: the name of the country in which this currency is used.Unfortunately, your parser didn't check the length of the codes and some erroneous data got into the table by mistake.
10 |Your task is to delete all rows from the currencies table in which code
is not exactly three letters long.
Example
12 |The following table currencies
13 |code | 16 |country | 17 |
---|---|
AD | 20 |Andorra | 21 |
AUD | 24 |Australia | 25 |
BRL | 28 |Brazil | 29 |
MXN | 32 |Mexico | 33 |
RUB | 36 |Russia | 37 |
RUR | 40 |Russia | 41 |
SEKR | 44 |Sweden | 45 |
USD | 48 |USA | 49 |
should become
52 |code | 55 |country | 56 |
---|---|
AUD | 59 |Australia | 60 |
BRL | 63 |Brazil | 64 |
MXN | 67 |Mexico | 68 |
RUB | 71 |Russia | 72 |
RUR | 75 |Russia | 76 |
USD | 79 |USA | 80 |
source: codesignal.com 3 |
You work with a professor of applied mathematics and informatics to create and distribute this year's plan of courses for students.
4 | You were almost finished when you noticed that a couple of fixes yet need to be done. There are courses which names' are marked as '-toremove'
, so you decided to remove them permanently. Prior to removing them, you'd like to make sure that deleting a course would also remove it from all the related tables automatically.
All courses are stored in courses table that has the following structure:
6 |id
: unique id of the course;name
: unique name of the course that may end with '-toremove'
, which means that the course is about to be removed;description
: description of the course.There are also groupcourses and groupexams tables which represent courses assigned to groups and examination dates of courses for each group, respectively. Here are their structures:
12 |group_id
: group id;course_id
: course id;date
: the date of the exam;group_id
: id of the group taking the exam;course_id
: id of the course.Given courses, groupcourses and groupexams tables you need to properly set up foreign keys, so that after a record is deleted from the courses table, records that correspond to the deleted course are also deleted from groupcourses and groupexams. Please note that groupexams
can only contain pairs from groupcourses
and there can be multiple exam dates for the same group_id
, course_id
combination. It is guaranteed that adding correct foreign keys won't raise any errors.
Example
29 |For the following tables courses
30 |id | 33 |name | 34 |description | 35 |
---|---|---|
1 | 38 |Linear algebra | 39 |Basis of matrix theory and linear algebra | 40 |
2 | 43 |Geometry | 44 |NULL | 45 |
3 | 48 |Determinants and matrices | 49 |NULL | 50 |
4 | 53 |Matlab-toremove | 54 |Matlab 7 | 55 |
groupcourses
58 |group_id | 61 |course_id | 62 |
---|---|
1 | 65 |1 | 66 |
1 | 69 |2 | 70 |
1 | 73 |3 | 74 |
2 | 77 |1 | 78 |
2 | 81 |4 | 82 |
and groupexams
85 |date | 88 |group_id | 89 |course_id | 90 |
---|---|---|
2010-01-10 | 93 |1 | 94 |1 | 95 |
2010-01-11 | 98 |1 | 99 |2 | 100 |
2010-01-12 | 103 |2 | 104 |4 | 105 |
the final result should be
108 |group_id | 111 |course_id | 112 |
---|---|
1 | 115 |1 | 116 |
1 | 119 |2 | 120 |
1 | 123 |3 | 124 |
2 | 127 |1 | 128 |
source: codesignal.com 3 |
You've just opened a registration for the online game you are about to finish developing.
4 | Everyone who wanted to reserve their nicknames for the release of the game had to submit their info using a special form on your website.
The reserved nicknames were stored in the reservedNicknames table with the following structure:
6 |id
: the unique id of the registered user;nickname
: the nickname submitted by the user with id id
.When you started going through these nicknames you realized that you forgot one important thing: all nicknames should contain exactly 8
characters.
11 | You have no time to fix this issue properly right now, so you decided to just add rename -
to the invalid nickname
s with corresponding id
s so it would be easier for you to deal with them latter.
Given the reservedNicknames table, change all rows with invalid nicknames by prepending rename -
to both nickname
and id
.
Example
14 |The following table reservedNicknames
15 |id | 18 |nickname | 19 |
---|---|
id001 | 22 |alex1990 | 23 |
id142 | 26 |killer007 | 27 |
id15674 | 30 |prohunter | 31 |
id4242 | 34 |acc0rdin | 35 |
id616 | 38 |Zoneg | 39 |
id9999 | 42 |butch | 43 |
should become
46 |id | 49 |nickname | 50 |
---|---|
id001 | 53 |alex1990 | 54 |
id4242 | 57 |acc0rdin | 58 |
rename - id142 | 61 |rename - killer007 | 62 |
rename - id15674 | 65 |rename - prohunter | 66 |
rename - id616 | 69 |rename - Zoneg | 70 |
rename - id9999 | 73 |rename - butch | 74 |
source: codesignal.com 3 |
As the head of a company you decided that it's time to introduce access control and to restrict access to employees table for all the employees. The table has the following structure:
4 |id
: unique employee id;name
: the name of the employee;date_joined
: the date the employee was hired;salary
: the salary of the employee;As a first step towards improving security you decided to create a view which would be used by the employees instead of direct access to the table.
11 |Create a view which will select all the columns from the employees table but leave only id
and name
intact. date_joined
column should contain the year the employee joined the company, and salary
column should be filled with -
(single dashes).
Example
13 |The following table employees
14 |id | 17 |name | 18 |date_joined | 19 |salary | 20 |
---|---|---|---|
1 | 23 |Farah Dahlquist | 24 |2006-03-05 | 25 |2000 | 26 |
2 | 29 |Yessenia Lininger | 30 |2008-04-11 | 31 |2500 | 32 |
3 | 35 |Justin Penrose | 36 |2009-12-12 | 37 |2400 | 38 |
4 | 41 |Josphine Clickner | 42 |2007-07-25 | 43 |3000 | 44 |
5 | 47 |Linwood Mcquiston | 48 |2011-01-15 | 49 |4000 | 50 |
the output should be
53 |id | 56 |name | 57 |date_joined | 58 |salary | 59 |
---|---|---|---|
1 | 62 |Farah Dahlquist | 63 |2006 | 64 |- | 65 |
2 | 68 |Yessenia Lininger | 69 |2008 | 70 |- | 71 |
3 | 74 |Justin Penrose | 75 |2009 | 76 |- | 77 |
4 | 80 |Josphine Clickner | 81 |2007 | 82 |- | 83 |
5 | 86 |Linwood Mcquiston | 87 |2011 | 88 |- | 89 |
source: codesignal.com 3 |
In order to optimize your business, you've decided to merge a couple of your office branches and get rid of the outdated branch types.
4 |The existing office branches and branch types are stored in the tables branches and branch_types, respectively. The tables have the following structures:
5 |branch_id
: the branch ID;name
: the name of the branch;branchtype_id
: the branch type ID.id
: the unique branch ID;name
: the name of the branch type.You've decided to start small. As your first step, you want to delete the outdated branch types, all of which end with -outdated
, from the branch_types table.
For now, you want to keep all the branches with the deleted branch types in the branches table, but to differentiate them you will set their branchtype_id
to NULL
. Update the database to make the required changes happen automatically when a record is deleted from branch_types.
Example
23 |For the following tables branches
24 |branch_id | 27 |name | 28 |branchtype_id | 29 |
---|---|---|
1 | 32 |Frankfurt branch | 33 |2 | 34 |
2 | 37 |Paris branch | 38 |3 | 39 |
3 | 42 |New York branch | 43 |2 | 44 |
4 | 47 |Madrid branch | 48 |1 | 49 |
and branch_types
52 |id | 55 |name | 56 |
---|---|
1 | 59 |Small-outdated | 60 |
2 | 63 |Big | 64 |
3 | 67 |Medium | 68 |
the output should be
71 |branch_id | 74 |name | 75 |branchtype_id | 76 |
---|---|---|
1 | 79 |Frankfurt branch | 80 |2 | 81 |
2 | 84 |Paris branch | 85 |3 | 86 |
3 | 89 |New York branch | 90 |2 | 91 |
4 | 94 |Madrid branch | 95 |NULL | 96 |
source: codesignal.com 3 |
You are managing a restaurant recommendation service that has recently started to gain popularity.
4 | The information about the restaurants is currently stored in the restaurants table with the following structure:
id
: unique id of the restaurant;name
: the restaurant's name.As the table grew you decided to extend it, in particular add the description
(VARCHAR(100)
, the description of the restaurant) and active
(INT
, 0
or 1
if it works or closed respectively) columns.
Add these two new columns to the restaurants table using the following rules for all records:
11 |description
should be set to "TBD"
(for To Be Defined).active
should be set to 1
.Example
16 |The following table restaurants
17 |id | 20 |name | 21 |
---|---|
1 | 24 |The Big City Barbecue | 25 |
2 | 28 |Roadhouse | 29 |
3 | 32 |Hibiscus | 33 |
4 | 36 |The Waterfront Courtyard | 37 |
5 | 40 |The Royal Spices | 41 |
should become
44 |id | 47 |name | 48 |description | 49 |active | 50 |
---|---|---|---|
1 | 53 |The Big City Barbecue | 54 |TBD | 55 |1 | 56 |
2 | 59 |Roadhouse | 60 |TBD | 61 |1 | 62 |
3 | 65 |Hibiscus | 66 |TBD | 67 |1 | 68 |
4 | 71 |The Waterfront Courtyard | 72 |TBD | 73 |1 | 74 |
5 | 77 |The Royal Spices | 78 |TBD | 79 |1 | 80 |
source: codesignal.com 3 |
At the beginning of this academic year you composed a list of clubs and a list of their members. The gathered information is stored in your local database in two tables that have the following structures:
4 |id
: unique club id;name
: club name;id
: unique student id;name
: student name;club_id
: id of the club the student joined.Each student can be a member only of a single club, and it is prohibited to drop a club until the year is over. However, several clubs were closed due to low attendance, and you need to update your database to reflect that. You have already updated the clubs table, and now the students table should be updated as well.
20 |Given the clubs and students tables, return a table containing the records from students that have club_id
equal to some id
of clubs. The table should be sorted by students' id
s.
Example
22 |For the following table students
23 |id | 26 |name | 27 |club_id | 28 |
---|---|---|
3 | 31 |Richard Gear | 32 |23 | 33 |
6 | 36 |Jennifer Offencerence | 37 |11 | 38 |
14 | 41 |Obiten Kenobi | 42 |87 | 43 |
36 | 46 |Closece | 47 |87 | 48 |
and clubs
51 |id | 54 |name | 55 |
---|---|
23 | 58 |Theater club | 59 |
64 | 62 |Soccer club | 63 |
87 | 66 |Glee club | 67 |
the output should be
70 |id | 73 |name | 74 |club_id | 75 |
---|---|---|
3 | 78 |Richard Gear | 79 |23 | 80 |
14 | 83 |Obiten Kenobi | 84 |87 | 85 |
36 | 88 |Closece | 89 |87 | 90 |
source: codesignal.com 3 |
You've just started working for a large and successful company, and it's come as a shock to find that the records about the company's internal organization haven't been updated for ages. Some employees have changed their departments and some have resigned, and consequently, there are some departments that have no employees in them. You want to identify these departments.
4 |Information about employees and departments is stored in two tables, employees and departments, which have the following structure:
5 |id
: the unique employee ID;full_name
: the employee's full name;department
: a foreign key referencing departments.id
;id
: the unique department ID;dep_name
: the department name.Given the tables employees and departments, build a table with only one column, dep_name
, which contains all the departments in which there are no employees, sorted by id
.
Example
22 |For the following tables departments
23 |id | 26 |dep_name | 27 |
---|---|
1 | 30 |IT | 31 |
2 | 34 |HR | 35 |
3 | 38 |Sales | 39 |
4 | 42 |Warehousing | 43 |
and employees
46 |id | 49 |full_name | 50 |department | 51 |
---|---|---|
1 | 54 |James Miller | 55 |1 | 56 |
2 | 59 |Joseph Harvey | 60 |1 | 61 |
3 | 64 |Anna Lawson | 65 |2 | 66 |
4 | 69 |Arthur Saunders | 70 |3 | 71 |
the output should be
74 |dep_name | 77 |
---|
Warehousing | 80 |
source: codesignal.com 3 |
Winter is coming, and it's going to be great! The weather forecasters promise that it's going to be snowy, and there are a bunch of holidays so you'll be able to spend entire days on the slopes. You want to know how many perfect ski days, which are sunny days that fall on holidays, there are going to be this winter.
4 |You downloaded information about the weather forecast and saved it as the table weather, which consists of a single sunny_date
column of type DATE
. Information about the upcoming holidays is stored in the table holidays, which consists of a holiday_date
column of type DATE
. It is guaranteed that the elements of each table are unique.
Given the holidays and weather tables, return the dates that are present in both (i.e., their intersection). The resulting table should consist of a single ski_date
column, and its elements should be sorted in ascending order.
Example
7 |For the following tables holidays
8 |holiday_date | 11 |
---|
2016-12-31 | 14 |
2017-01-02 | 17 |
2017-01-03 | 20 |
2017-01-20 | 23 |
and weather
26 |sunny_date | 29 |
---|
2016-12-31 | 32 |
2016-12-25 | 35 |
2017-02-01 | 38 |
2017-01-02 | 41 |
2016-11-27 | 44 |
2017-01-01 | 47 |
the output should be
50 |ski_date | 53 |
---|
2016-12-31 | 56 |
2017-01-02 | 59 |
The dates in this example are given in YYYY-MM-DD
format.
source: codesignal.com 3 |
You are playing a game on a rectangular checkerboard with a specific set of rules.
4 | Each game piece has a unique id
and each cell on the board is defined by its x
and y
coordinates.
The current positions of your pieces on the board are stored in the positions table with the following structure:
6 |id
: unique piece id;x
: x coordinate of the cell that the piece with id id
occupies;y
: y coordinate of the cell that the piece with id id
occupies.In this game each piece on the board is said to defend its nearest neighbor. The distance between two pieces is calculated simply as the distance between the points (x1, y1)
and (x2, y2)
, where (x1, y1)
and (x2, y2)
are the coordinates of the cells occupied by the first and the second piece, respectively.
12 | You thought it might be good idea to find what piece each of the game pieces defends.
Given the positions table, compose the resulting table with two columns: id1
and id2
, such that on each row the piece with id id1
defends the piece with id2
(i.e. id2
is the closest to id1
piece).
14 | The table should be sorted by the values of id1
in ascending order.
15 | It's guaranteed that for each piece there is only one other piece closest to it.
Example
17 |For the following table positions
18 |id | 21 |x | 22 |y | 23 |
---|---|---|
1 | 26 |0 | 27 |0 | 28 |
2 | 31 |2 | 32 |0 | 33 |
3 | 36 |4 | 37 |3 | 38 |
4 | 41 |2 | 42 |1 | 43 |
the output should be
46 |id1 | 49 |id2 | 50 |
---|---|
1 | 53 |2 | 54 |
2 | 57 |4 | 58 |
3 | 61 |4 | 62 |
4 | 65 |2 | 66 |
source: codesignal.com 3 |
You're doing research about academic achievement, and you're studying one particular group of students at a local university. Find the average grade of the five most successful students in this group.
4 |The information about the students' grades is stored in a table called students, which has the following attributes:
5 |student_id
: the unique identifier of the student;student_name
: the name of the student;grade
: the grade that the student achieved.The result should be a table with one column average_grade
and one row that contains the average grade of the five students with the highest grades. The answer should be formatted to 2
digits after the decimal point. It is guaranteed that the five most successful students can be uniquely identified.
Example
12 |For the following table students
13 |student_id | 16 |student_name | 17 |grade | 18 |
---|---|---|
1 | 21 |Oliver Smith | 22 |3.2 | 23 |
2 | 26 |Jacob Bell | 27 |2.9 | 28 |
3 | 31 |William Thompson | 32 |3.1 | 33 |
4 | 36 |Sophie Clark | 37 |3.5 | 38 |
5 | 41 |Daniel Palmer | 42 |3.6 | 43 |
6 | 46 |Emily Morris | 47 |4.0 | 48 |
7 | 51 |Zachary Mills | 52 |2.5 | 53 |
the output should be
56 |average_grade | 59 |
---|
3.48 | 62 |
The top 5 grades for this group are 4.0
, 3.6
, 3.5
, 3.2
, and 3.1
, so their sum is equal to 17.4
and 17.4 / 5 = 3.48
.
source: codesignal.com 3 |
There are many employees at your company, with a wide range of salaries. You've decided to find out the difference in salary between the employees who make the most and the employees who make the least.
4 |You store information about your employees in the table employees, which has the structure:
5 |id
: the unique employee ID;name
: the employee's name;salary
: the employee's salary - a positive integer.The difference between the sum of the highest salaries and the sum of lowest salaries will give you the information you want. So, given the table employees, build the resulting table as follows: The table should contain only one column difference
and only one row, which will contain the difference between sum of the highest and the sum of lowest salaries. If employees table is empty, the difference should be equal 0
.
Example
12 |For the following table employees
13 |id | 16 |name | 17 |salary | 18 |
---|---|---|
1 | 21 |John | 22 |1200 | 23 |
2 | 26 |Bill | 27 |1000 | 28 |
3 | 31 |Mike | 32 |1300 | 33 |
4 | 36 |Katy | 37 |1200 | 38 |
5 | 41 |Brendon | 42 |1300 | 43 |
6 | 46 |Amanda | 47 |900 | 48 |
the output should be
51 |difference | 54 |
---|
1700 | 57 |
There are two highest salaries (1300
) and one lowest salary (900
). 1300 * 2 - 900 * 1 = 1700
.
source: codesignal.com 3 |
Your company has many different departments. Information about the employees working in these departments is stored in the pr_department, it_department, and sales_department tables, which have the following structures:
4 |id
: the unique (for this department) employee ID;name
: the name of the employee;date_joined
: the date this employee was hired by your company.id
: the unique (for this department) employee ID;name
: the name of the employee;date_joined
: the date this employee was hired by your company.id
: the unique (for this department) employee ID;name
: the name of the employee;date_joined
: the date this employee was hired by your company.Your task is to prepare a report that has the names of all the employees who were hired recently.
28 |Given the pr_department, it_department, and sales_department tables, compose the result as follows: The resulting table should have one column names
that contains the names of the last five people to join each department. The names of the employees from the pr_department should come first, followed by the it_department, and the sales_department will come last. The names of employees who work in the same department should be sorted in ascending order.
Example
30 |The following tables pr_department
31 |id | 34 |name | 35 |date_joined | 36 |
---|---|---|
2 | 39 |Bertha Neiman | 40 |2010-12-15 | 41 |
3 | 44 |Melissa Chace | 45 |2012-08-14 | 46 |
4 | 49 |Doyle Wiste | 50 |2015-01-02 | 51 |
8 | 54 |Libbie Troy | 55 |2016-03-04 | 56 |
15 | 59 |Noreen Bonner | 60 |2012-07-07 | 61 |
42 | 64 |Farah Dahlquist | 65 |2006-12-12 | 66 |
it_department
69 |id | 72 |name | 73 |date_joined | 74 |
---|---|---|
1 | 77 |Farah Dahlquist | 78 |2017-01-15 | 79 |
2 | 82 |Mohammed Claycomb | 83 |2016-06-11 | 84 |
3 | 87 |Allen Reger | 88 |2015-08-12 | 89 |
5 | 92 |Jacque Fuhr | 93 |2013-04-01 | 94 |
4 | 97 |Joya Buss | 98 |2011-11-11 | 99 |
6 | 102 |Leonia Maultsby | 103 |2009-02-02 | 104 |
7 | 107 |Oliva Mcgarry | 108 |2008-05-05 | 109 |
and sales_department
112 |id | 115 |name | 116 |date_joined | 117 |
---|---|---|
3 | 120 |Jayne Kravitz | 121 |2009-09-09 | 122 |
4 | 125 |Joya Buss | 126 |2008-08-08 | 127 |
7 | 130 |Edmond Kind | 131 |2007-07-07 | 132 |
the output should be
135 |names | 138 |
---|
Bertha Neiman | 141 |
Doyle Wiste | 144 |
Libbie Troy | 147 |
Melissa Chace | 150 |
Noreen Bonner | 153 |
Allen Reger | 156 |
Farah Dahlquist | 159 |
Jacque Fuhr | 162 |
Joya Buss | 165 |
Mohammed Claycomb | 168 |
Edmond Kind | 171 |
Jayne Kravitz | 174 |
Joya Buss | 177 |
source: codesignal.com 3 |
Your boss knows that you're a good programmer and that you're responsible, so she's given you a big task - to check the company's expenditure automatically. You're given the table expenditure_plan, which describes all the planned expenditures, and the table allowable_expenditure, which describes the amount that can be spent in certain time periods.
4 |The expenditure_plan table contains the following columns:
5 |monday_date
- the unique date of the Monday of the corresponding week;expenditure_sum
- the sum of the planned expenditure for the corresponding week.The allowable_expenditure table contains the following columns:
11 |id
- the unique ID of the limitation;left_bound
- the unique left bound of the time period, represented by the number of the week (1
-based);right_bound
- the unique right bound of the time period, represented by the number of the week (1
-based);value
- the allowable sum that can be spent during the given left and right bounds, inclusive.1
is the first full week of the year, meaning that its Monday is part of the current year.Your task is to return a table with the columns id
and loss
, where the value of loss
is either 0
(if the expenditure limit with this id
was not exceeded) or the amount of money by which the planned expenditure exceeded the allowable expenditure, sorted by id
.
Example
20 |For given table expenditure_plan
21 |monday_date | 24 |expenditure_sum | 25 |
---|---|
2016-02-08 | 28 |10 | 29 |
2016-02-15 | 32 |12 | 33 |
2016-06-13 | 36 |5 | 37 |
2016-06-27 | 40 |13 | 41 |
and table allowable_expenditure
44 |id | 47 |left_bound | 48 |right_bound | 49 |value | 50 |
---|---|---|---|
1 | 53 |5 | 54 |8 | 55 |30 | 56 |
2 | 59 |23 | 60 |26 | 61 |10 | 62 |
the output should be
65 |id | 68 |loss | 69 |
---|---|
1 | 72 |0 | 73 |
2 | 76 |8 | 77 |
In the table expenditure_plan, the Mondays correspond to the weeks with numbers 6
, 7
, 24
, and 26
respectively. For the first time period, we add up 10
and 12
to get 22
. Since 22
is smaller than 30
, the loss
is 0
. For the second time period, we add up 5
and 13
to get 18
. Since 18
is greater than 10
, the allowable expendature for this time period, the answer is 18 - 10 = 8
.
source: codesignal.com 3 |
During the dance contest, each judge evaluates the dancers' performances based on three criteria, using a score from 1
to 10
for each of the criteria. You are given a table of the scores
awarded to a specific dancer by each judge. Recently, the dance contest made the decision to drop scores awarded by a certain judge if that judge gave an extreme (either minimum or maximum) score for at least two criteria.
For example, if the judge awarded one of the minimum scores for musicality (i.e., there may be similar scores for musicality, but there may not be smaller scores for that criterion) and one of the maximum scores for floorcraft, all three of the scores given by that judge should not be taken into account.
5 |Return a table that consists of only the scores that should be considered after this filtering, sorted by arbiter_id
.
The scores table contain the following columns:
7 |arbiter_id
- the unique ID of the judge;first_criterion
- the score given for the first criterion;second_criterion
- the score given for the second criterion;third_criterion
- the score given for the third criterion.Example
14 |For given table scores
15 |arbiter_id | 18 |first_criterion | 19 |second_criterion | 20 |third_criterion | 21 |
---|---|---|---|
1 | 24 |3 | 25 |10 | 26 |10 | 27 |
2 | 30 |2 | 31 |3 | 32 |4 | 33 |
3 | 36 |5 | 37 |6 | 38 |7 | 39 |
4 | 42 |2 | 43 |5 | 44 |9 | 45 |
5 | 48 |2 | 49 |2 | 50 |2 | 51 |
the output should be
54 |arbiter_id | 57 |first_criterion | 58 |second_criterion | 59 |third_criterion | 60 |
---|---|---|---|
2 | 63 |2 | 64 |3 | 65 |4 | 66 |
3 | 69 |5 | 70 |6 | 71 |7 | 72 |
4 | 75 |2 | 76 |5 | 77 |9 | 78 |
The first judge gave the maximal scores for the second and third criteria, so his scores aren't included in the answer. The fifth judge's given scores are all minimal, so her scores aren't included to the answer either.
81 |source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /64-trackingSystem/trackingSystem.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/64-trackingSystem/trackingSystem.sql -------------------------------------------------------------------------------- /65-storageOptimization/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /65-storageOptimization/storageOptimization.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/65-storageOptimization/storageOptimization.sql -------------------------------------------------------------------------------- /66-consecutiveIds/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /66-consecutiveIds/consecutiveIds.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/66-consecutiveIds/consecutiveIds.sql -------------------------------------------------------------------------------- /67-holidayEvent/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /67-holidayEvent/holidayEvent.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/67-holidayEvent/holidayEvent.sql -------------------------------------------------------------------------------- /68-hostnamesOrdering/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /68-hostnamesOrdering/hostnamesOrdering.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/68-hostnamesOrdering/hostnamesOrdering.sql -------------------------------------------------------------------------------- /69-orderAnalytics/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /69-orderAnalytics/orderAnalytics.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/69-orderAnalytics/orderAnalytics.sql -------------------------------------------------------------------------------- /70-customerMessages/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /70-customerMessages/customerMessages.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/70-customerMessages/customerMessages.sql -------------------------------------------------------------------------------- /71-orderPrices/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /71-orderPrices/orderPrices.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/71-orderPrices/orderPrices.sql -------------------------------------------------------------------------------- /72-findTable/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /72-findTable/findTable.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/72-findTable/findTable.sql -------------------------------------------------------------------------------- /73-queriesExecution/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /73-queriesExecution/queriesExecution.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/73-queriesExecution/queriesExecution.sql -------------------------------------------------------------------------------- /74-filmLibrary/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /74-filmLibrary/filmLibrary.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/74-filmLibrary/filmLibrary.sql -------------------------------------------------------------------------------- /75-bugsInComponent/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /75-bugsInComponent/bugsInComponent.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/75-bugsInComponent/bugsInComponent.sql -------------------------------------------------------------------------------- /76-freeSeats/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /76-freeSeats/freeSeats.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/76-freeSeats/freeSeats.sql -------------------------------------------------------------------------------- /77-giftPackaging/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /77-giftPackaging/giftPackaging.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/77-giftPackaging/giftPackaging.sql -------------------------------------------------------------------------------- /78-stringsStatistics/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /78-stringsStatistics/stringsStatistics.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/78-stringsStatistics/stringsStatistics.sql -------------------------------------------------------------------------------- /79-unluckyEmployees/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /79-unluckyEmployees/unluckyEmployees.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/79-unluckyEmployees/unluckyEmployees.sql -------------------------------------------------------------------------------- /80-driversInfo/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /80-driversInfo/driversInfo.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/80-driversInfo/driversInfo.sql -------------------------------------------------------------------------------- /81-sortBookChapters/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /81-sortBookChapters/sortBookChapters.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/81-sortBookChapters/sortBookChapters.sql -------------------------------------------------------------------------------- /82-typeInheritance/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /82-typeInheritance/typeInheritance.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/82-typeInheritance/typeInheritance.sql -------------------------------------------------------------------------------- /83-battleshipGameResults/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /83-battleshipGameResults/battleshipGameResults.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/83-battleshipGameResults/battleshipGameResults.sql -------------------------------------------------------------------------------- /84-tictactoeTournament/README.md: -------------------------------------------------------------------------------- 1 |
source: codesignal.com 3 | 4 | -------------------------------------------------------------------------------- /84-tictactoeTournament/tictactoeTournament.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waq-r/CodeSignal-Databases/86ed893c93dccd8f954f696978302acd4c56e51e/84-tictactoeTournament/tictactoeTournament.sql --------------------------------------------------------------------------------