|
| 1 | +/*Query 1 |
| 2 | +Get the id values of the first 5 clients |
| 3 | +from district_id with a value equals to 1.*/ |
| 4 | +SELECT client_id |
| 5 | +FROM client |
| 6 | +WHERE district_id = 1 |
| 7 | +ORDER BY client_id ASC |
| 8 | +LIMIT 5; |
| 9 | + |
| 10 | +/*Query 2 |
| 11 | +In the client table, get an id value of |
| 12 | +the last client where the district_id equals to 72.*/ |
| 13 | +SELECT client_id |
| 14 | +FROM client |
| 15 | +WHERE district_id = 72 |
| 16 | +ORDER BY client_id DESC |
| 17 | +LIMIT 1; |
| 18 | + |
| 19 | +/*Query 3 |
| 20 | +Get the 3 lowest amounts in the loan table*/ |
| 21 | +SELECT amount |
| 22 | +FROM loan |
| 23 | +ORDER BY amount ASC |
| 24 | +LIMIT 3; |
| 25 | + |
| 26 | +/*Query 4 |
| 27 | +What are the possible values for status, |
| 28 | +ordered alphabetically in ascending order in the loan table?*/ |
| 29 | +SELECT DISTINCT status |
| 30 | +FROM loan |
| 31 | +ORDER BY status ASC ; |
| 32 | + |
| 33 | +/*Query 5 |
| 34 | +What is the loan_id of the highest payment received in the loan table?*/ |
| 35 | +SELECT loan_id |
| 36 | +FROM loan |
| 37 | +ORDER BY payments DESC |
| 38 | +LIMIT 1; |
| 39 | + |
| 40 | +/*Query 6 |
| 41 | +What is the loan amount of the lowest 5 account_ids in the loan table? |
| 42 | +Show the account_id and the corresponding amount*/ |
| 43 | +SELECT account_id, amount |
| 44 | +FROM loan |
| 45 | +ORDER BY account_id ASC |
| 46 | +LIMIT 5; |
| 47 | + |
| 48 | +/*Query 7 |
| 49 | +What are the account_ids with the lowest loan amount |
| 50 | +that have a loan duration of 60 in the loan table?*/ |
| 51 | +SELECT account_id |
| 52 | +FROM loan |
| 53 | +WHERE duration = 60 |
| 54 | +ORDER BY amount ASC |
| 55 | +LIMIT 5; |
| 56 | + |
| 57 | +/*Query 8 |
| 58 | +What are the unique values of k_symbol in the order table? |
| 59 | +
|
| 60 | +Note: There shouldn't be a table name order, |
| 61 | +since order is reserved from the ORDER BY clause. |
| 62 | +You have to use backticks to escape the order table name.*/ |
| 63 | +SELECT DISTINCT k_symbol |
| 64 | +FROM 'order' |
| 65 | +ORDER BY k_symbol ASC ; |
| 66 | + |
| 67 | +/*Query 9 |
| 68 | +In the order table, what are the order_ids of |
| 69 | +the client with the account_id 34?*/ |
| 70 | +SELECT order_id |
| 71 | +FROM 'order' |
| 72 | +WHERE account_id = 34; |
| 73 | + |
| 74 | +/*Query 10 |
| 75 | +In the order table, which account_ids were responsible for orders |
| 76 | +between order_id 29540 and order_id 29560 (inclusive)?*/ |
| 77 | +SELECT DISTINCT account_id |
| 78 | +FROM 'order' |
| 79 | +WHERE order_id>=29540 and order_id<=29560 ; |
| 80 | + |
| 81 | +/*Query 11 |
| 82 | +In the order table, what are the individual amounts |
| 83 | +that were sent to (account_to) id 30067122?*/ |
| 84 | +SELECT amount |
| 85 | +FROM 'order' |
| 86 | +WHERE account_to = 30067122; |
| 87 | + |
| 88 | +/*Query 12 |
| 89 | +In the trans table, show the trans_id, |
| 90 | +date, type and amount of |
| 91 | +the 10 first transactions |
| 92 | +from account_id 793 in chronological order, |
| 93 | +from newest to oldest.*/ |
| 94 | +SELECT trans_id, date, type, amount |
| 95 | +FROM trans |
| 96 | +WHERE account_id = 793 |
| 97 | +ORDER BY date DESC |
| 98 | +LIMIT 10; |
| 99 | + |
| 100 | +/*Query 13 |
| 101 | +In the client table, of all districts with a district_id lower than 10, |
| 102 | +how many clients are from each district_id? |
| 103 | +Show the results sorted by the district_id in ascending order.*/ |
| 104 | +SELECT district_id, COUNT(client_id) AS client_count |
| 105 | +FROM client |
| 106 | +WHERE district_id < 10 |
| 107 | +GROUP BY district_id |
| 108 | +ORDER BY district_id ASC; |
| 109 | + |
| 110 | +/*Query 14 |
| 111 | +In the card table, how many cards exist for each type? |
| 112 | +Rank the result starting with the most frequent type.*/ |
| 113 | +SELECT type,COUNT(card_id) as card_freq |
| 114 | +FROM card |
| 115 | +GROUP BY type |
| 116 | +ORDER BY card_freq DESC; |
| 117 | + |
| 118 | +/*Query 15 |
| 119 | +Using the loan table, print the top 10 account_ids |
| 120 | +based on the sum of all of their loan amounts.*/ |
| 121 | +SELECT account_id, sum(amount) as loan_amount |
| 122 | +FROM loan |
| 123 | +GROUP BY account_id |
| 124 | +ORDER BY loan_amount DESC |
| 125 | +LIMIT 10; |
| 126 | + |
| 127 | +/*Query 16 |
| 128 | +In the loan table, retrieve the number of loans issued for each day, before (excl) 930907, |
| 129 | +ordered by date in descending order*/ |
| 130 | +SELECT date, COUNT(*) AS loan_count |
| 131 | +FROM loan |
| 132 | +WHERE date < 930907 |
| 133 | +GROUP BY date |
| 134 | +ORDER BY date DESC; |
| 135 | + |
| 136 | +/*Query 17 |
| 137 | +In the loan table, for each day in December 1997, |
| 138 | +count the number of loans issued for each unique loan duration, |
| 139 | +ordered by date and duration, both in ascending order. |
| 140 | +You can ignore days without any loans in your output*/ |
| 141 | +SELECT date, duration, COUNT(*) AS loan_count |
| 142 | +FROM loan |
| 143 | +WHERE date BETWEEN 971201 AND 971231 |
| 144 | +GROUP BY date, duration |
| 145 | +ORDER BY date ASC, duration ASC; |
| 146 | + |
| 147 | + /*Query 18 |
| 148 | +In the trans table, for account_id 396, |
| 149 | +sum the amount of transactions for each type (VYDAJ = Outgoing, PRIJEM = Incoming). |
| 150 | +Your output should have the account_id, |
| 151 | +the type and the sum of amount, named as total_amount. |
| 152 | +Sort alphabetically by type.*/ |
| 153 | +SELECT account_id, type, SUM(amount) AS total_amount |
| 154 | +FROM trans |
| 155 | +WHERE account_id = 396 |
| 156 | +GROUP BY account_id, type |
| 157 | +ORDER BY type ASC; |
| 158 | + |
| 159 | + |
0 commit comments