Codewars Flashcards

1
Q

This kata is about multiplying a given number by eight if it is an even number and by nine otherwise. Db: multiplication

A

SELECT number,
CASE
WHEN number%2=0 THEN number * 8
ELSE number * 9
END
AS res
FROM multiplication

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In your application, there is a section for adults only. You need to get a list of names and ages of users from the users table, who are 18 years old or older.

users table schema

name
age

A

SELECT name, age
FROM users
WHERE age >= 18

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

You are a border guard sitting on the Canadian border. You were given a list of travelers who have arrived at your gate today. You know that American, Mexican, and Canadian citizens don’t need visas, so they can just continue their trips. You don’t need to check their passports for visas! You only need to check the passports of citizens of all other countries!

Select names, and countries of origin of all the travelers, excluding anyone from Canada, Mexico, or The US.

travelers table schema

name
country

A

SELECT name, country
FROM travelers
WHERE country NOT IN (‘Canada’, ‘Mexico’, ‘USA’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

You received an invitation to an amazing party. Now you need to write an insert statement to add yourself to the table of participants.

participants table schema

name (string)
age (integer)
attending (boolean)

A

INSERT into participants (name, age, attending) values (‘Eric’, 21, true);

SELECT * FROM participants;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a select statement to get a list of all students who haven’t paid their tuition yet. The list should include all the data available about these students.

students table schema

name (string)
age (integer)
semester (integer)
mentor (string)
tuition_received (Boolean)

A

SELECT *
FROM students
WHERE tuition_received = FALSE;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

You work at a book store. It’s the end of the month, and you need to find out the 5 bestselling books at your store. Use a select statement to list names, authors, and number of copies sold of the 5 books which were sold most.

books table schema

name
author
copies_sold

A

select name, author, copies_sold
from books
order by copies_sold desc
limit 5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

from the 3 African countries (might also be called Afrika) that start with the character E, get the names of their capitals ordered alphabetically.

Schema of the countries table:

country (String)
capital (String)
continent (String)

A

SELECT capital
FROM countries
WHERE continent IN (‘Africa’, ‘Afrika’)
AND country LIKE ‘E%’
ORDER BY capital
LIMIT 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In SQL, you will be given a table years with a column yr for the year. Return a table with a column century.
The first century spans from the year 1 up to and including the year 100, the second century - from the year 101 up to and including the year 200, etc.

A

select yr, ((yr-1)/100)+1 as century
from years

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

You will be given a table numbers, with one column number.

Return a dataset with two columns: number and is_even, where number contains the original input value, and is_even containing “Even” or “Odd” depending on number column values

A

SELECT number,
CASE
WHEN number % 2 = 0 THEN ‘Even’
ELSE ‘Odd’
END AS is_even
FROM numbers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Given a month as an integer from 1 to 12, return to which quarter of the year it belongs as an integer number.

For example: month 2 (February), is part of the first quarter; month 6 (June), is part of the second

A

select month, cast(ceiling(month/3.0)as int) as res
from quarterof

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Return a table with two columns (number1, number2), the value in number1 should be rounded down and the value in number2 should be rounded up.
** decimals table schema **

id
number1
number2

A

select floor(number1) as number1,
ceiling(number2) as number2
from decimals

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Your classmates asked you to copy some paperwork for them. You know that there are ‘n’ classmates and the paperwork has ‘m’ pages.

Your task is to calculate how many blank pages do you need. db:paperwork

A

SELECT n, m,
CASE WHEN n < 0 OR m < 0 THEN 0
ELSE n*m
END AS res
FROM paperwork;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Clock shows h hours, m minutes and s seconds after midnight.

Your task is to write a function which returns the time since midnight in milliseconds. db:past

A

SELECT ((h60 + m)60+ s )*1000 as res

FROM past

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a function that removes the spaces from the string (x), then return the resultant string as res. db:nospace

A

SELECT x, REPLACE(x, ‘ ‘, ‘’) AS res
FROM nospace

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Return a table with one column (mod) which is the output of number1 modulus number2 from the decimals table

A

select mod(number1, number2) as mod
from decimals

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

find duplicates

A

SELECT
column_name,
COUNT() AS count
FROM
your_table_name
GROUP BY
column_name
HAVING
COUNT(
) > 1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

write a function that takes an argument and returns the square of it. db:square, n:1

A

SELECT n, n * n AS res FROM square;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

use the applications table, where each application has a status represented by the application_status column. A status value of 5 or lower indicates a rejected application, while a status value greater than 5 indicates an approved application.
expected output: statusgroup, applicationnum

A

SELECT
‘Approved’ AS status_group,
COUNT(application_id) FILTER (WHERE application_status > 5) AS application_num
FROM applications
UNION
SELECT
‘Rejected’ AS status_group,
COUNT(application_id) FILTER (WHERE application_status <= 5) AS application_num
FROM applications
ORDER BY status_group DESC;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Find the total number of interview failures grouped by the failure_reason in the interview_failures table without using the COUNT() function or relying on any auto-incrementing id column.

The result set should contain the following columns:
failure_reason (varchar): reason for the interview failure
cnt (int): count of the employees who failed because of that reason

Order the results by cnt in descending order. If the count is the same for multiple reasons, order them alphabetically by the reason.

A

select
failure_reason,
sum(1) as cnt
from interview_failures
group by failure_reason
order by 2 desc, 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Returns records that have matching values in both tables

A

SQL INNER JOIN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Returns all records from the left table, and the matched records from the right table

A

Left Join

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Returns all records when there is a match in either left or right table

A

Full Join

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Table years has two columns: id, and year.
Your query has to return rows with two columns: year, and is_leap. Find the leap years
Returned rows have to be sorted ascending by the year.
Years divisible by 4 are leap years,
but years divisible by 100 are not leap years,
but years divisible by 400 are leap years.

A

select
year,
case
when year%400=0 then true
when year%100=0 then false
when year%4=0 then true
else false
end as is_leap
from years
order by year;

or

select
year,
(year % 4 = 0 and year % 100 <> 0 ) or ( year % 400 = 0 ) as is_leap – your code here
from years
order by year asc;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

the nearest pump is 50 miles away! You know that on average, your car runs on about 25 miles per gallon. There are 2 gallons left.

Considering these factors, write a function that tells you if it is possible to get to the pump or not.

Function should return true if it is possible and false if not.
db: zerofuel, distance_to_pump, fuel_left

A

SELECT distance_to_pump, mpg, fuel_left,
CASE
WHEN mpg * fuel_left < distance_to_pump THEN False
ELSE True
END
AS res
FROM zerofuel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

join the two tables Orders and Customers, using the CustomerID field in both tables as the relationship between the two tables.

A

SELECT *
FROM Orders
LEFT JOIN Customers
ON Orders.CustomerID
=
Customers.CustomerID

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

join the two tables Orders and Customers, using the CustomerID field in both tables as the relationship between the two tables.

A

SELECT *
FROM Orders
LEFT JOIN Customers
ON Orders.CustomerID= Customers.CustomerID

27
Q

Select all records from the Customers table, sort the result alphabetically by the column City.

A

SELECT * FROM Customers
order by city;

28
Q

Select all records from the Customers table, sort the result alphabetically, first by the column Country, then, by the column City.

A

SELECT * FROM Customers
ORDER BY Country, City;

29
Q

Select all records where the City column has the value ‘Berlin’ and the PostalCode column has the value ‘12209’.

A

select* FROM Customers
where City = ‘Berlin’
and postalcode = ‘12209’;

30
Q

Use the NOT keyword to select all records where City is NOT “Berlin”.

A

SELECT * FROM Customers
WHERE NOT City = ‘Berlin’;

31
Q

Insert a new record in the following Customers table:
CustomerName,
Address,
City,
PostalCode,
Country

A

insert into
Customers
(CustomerName,
Address,
City,
PostalCode,
Country)
values (‘Hekkan Burger’,
‘Gateveien 15’,
‘Sandnes’,
‘4306’,
‘Norway’);

32
Q

select all records from the Customers where the PostalCode column is empty.

A

SELECT * FROM Customers
WHERE postalcode is null;

33
Q

Select all records from the Customers where the PostalCode column is NOT empty.

A

SELECT * FROM Customers
WHERE postalcode is not null;

34
Q

Update the City column of all records to Oslo in the Customers table.

A

UPDATE Customers
SET City = ‘Oslo’;

35
Q

Set the value of the City columns to ‘Oslo’, but only the ones where the Country column has the value “Norway”

A

update Customers
set City = ‘Oslo’
where Country = ‘Norway’;

36
Q

Update the City value and the Country value for customer id 32

A

update Customers
set City = ‘Oslo’,
country = ‘Norway’
WHERE CustomerID = 32;

37
Q

Delete all the records from the Customers table where the Country value is ‘Norway’.

A

DELETE FROM Customers
WHERE Country = ‘Norway’;

38
Q

Delete all the records from the Customers table.

A

delete from Customers;

39
Q

Use the MIN function to select the record with the smallest value of the Price column.

A

SELECT MIN(Price)
FROM Products;

40
Q

Use an SQL function to select the record with the highest value of the Price column.

A

SELECT max(price)
FROM Products;

41
Q

Use the correct function to return the number of records from Products that have the Price value set to 18

A

SELECT COUNT(*)
FROM Products
WHERE Price = 18;

42
Q

Use an SQL function to calculate the average Price of all products.

A

SELECT avg(price)
FROM Products;

43
Q

Select all records where the value of the City column starts with the letter “a”.

A

SELECT * FROM Customers
WHERE City LIKE ‘a%’;

44
Q

Select all records where the value of the City column ends with the letter “a”.

A

SELECT * FROM Customers
where city like ‘%a’;

45
Q

Select all records where the value of the City column contains the letter “a”.

A

SELECT * FROM Customers
WHERE City LIKE ‘%a%’;

46
Q

Select all records where the value of the City column starts with letter “a” and ends with the letter “b”.

A

SELECT * FROM Customers
where city like ‘a%b’;

47
Q

Select all records where the value of the City column does NOT start with the letter “a”.

A

SELECT * FROM Customers
where city not like ‘a%’;

48
Q

Select all records where the second letter of the City is an “a”.

A

SELECT * FROM Customers
WHERE City LIKE ‘_a%’;

49
Q

Select all records from customers where the first letter of the City is an “a” or a “c” or an “s”.

A

SELECT * FROM Customers
WHERE City LIKE ‘[acs]%’;

50
Q

Select all records where the first letter of the City starts with anything from an “a” to an “f”.

A

SELECT * FROM Customers
WHERE City LIKE ‘[a-f]%’;

51
Q

Select all records where the first letter of the City is NOT an “a” or a “c” or an “f”.

A

SELECT * FROM Customers
WHERE City LIKE ‘[!acf]%’;

52
Q

Use the IN operator to select all the records where Country is either “Norway” or “France”.

A

SELECT * FROM Customers
where country in (‘Norway’,
‘France’);

53
Q

Use the IN operator to select all the records where Country is NOT “Norway” and NOT “France”.

A

SELECT * FROM Customers
where country not in (‘Norway’, ‘France’);

54
Q

select all the records where the value of the Price column is between 10 and 20.

A

SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

55
Q

select all the records where the value of the Price column is NOT between 10 and 20.

A

SELECT * FROM Products
WHERE Price
not between 10 and 20;

56
Q

select all the records where the value of the ProductName column is alphabetically between ‘Geitost’ and ‘Pavlova’.

A

SELECT * FROM Products
WHERE ProductName between ‘Geitost’ and ‘Pavlova’;

57
Q

List the number of customers from ‘Customers’ in each country.

A

SELECT count (CustomerID), Country
FROM Customers
group by country;

58
Q

List the number of customers in each country, ordered by the country with the most customers first.

A

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY
COUNT(CustomerID) DESC;

59
Q

Write the correct SQL statement to create a new database called testDB.

A

create database testDB;

60
Q

Write the correct SQL statement to delete a database named testDB.

A

DROP DATABASE testDB;

61
Q

Write the correct SQL statement to create a new table called Persons with the following columns
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)

A

create table persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

62
Q

Add a column of type DATE called Birthday.

A

alter table Persons
add birthday date;

63
Q

Delete the column Birthday from the Persons table.

A

alter table Persons
drop column Birthday;

64
Q

What’s the SQL order of execution?

A
  1. From
  2. Join
  3. Where- filter rows
  4. Group By
  5. Having- filters the group from group by
  6. Select
  7. Distinct
  8. Order by
  9. Limit/Offset