Newer
Older
"\n",
" loan_purpose income \n",
"0 1 None \n",
"1 1 None \n",
"2 1 None \n",
"3 1 None \n",
"4 2 None "
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# what are the five biggest loans in terms of dollar amount? Practice ORDER BY.\n",
"pd.read_sql(\"\"\"\n",
"SELECT *\n",
"FROM loans\n",
"ORDER BY loan_amount DESC\n",
"LIMIT 5\n",
"\"\"\", conn)"
]
},
{
"cell_type": "code",
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
"id": "c01b796c-ded3-4e11-81e7-69d7660da9cb",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>action_taken</th>\n",
" <th>loan_type</th>\n",
" <th>lei</th>\n",
" <th>loan_amount</th>\n",
" <th>interest_rate</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Loan purchased by the institution</td>\n",
" <td>Conventional</td>\n",
" <td>549300XWUSRVVOHPRY47</td>\n",
" <td>264185000.0</td>\n",
" <td>NA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Loan originated</td>\n",
" <td>Conventional</td>\n",
" <td>AD6GFRVSDT01YPT1CS68</td>\n",
" <td>74755000.0</td>\n",
" <td>1.454</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Application withdrawn by applicant</td>\n",
" <td>FHA-insured</td>\n",
" <td>AD6GFRVSDT01YPT1CS68</td>\n",
" <td>66005000.0</td>\n",
" <td>NA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Loan originated</td>\n",
" <td>Conventional</td>\n",
" <td>YQI2CPR3Z44KAR0HG822</td>\n",
" <td>65005000.0</td>\n",
" <td>3.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Loan originated</td>\n",
" <td>FHA-insured</td>\n",
" <td>254900YA1AQXNM8QVZ06</td>\n",
" <td>63735000.0</td>\n",
" <td>2.99</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" action_taken loan_type lei \\\n",
"0 Loan purchased by the institution Conventional 549300XWUSRVVOHPRY47 \n",
"1 Loan originated Conventional AD6GFRVSDT01YPT1CS68 \n",
"2 Application withdrawn by applicant FHA-insured AD6GFRVSDT01YPT1CS68 \n",
"3 Loan originated Conventional YQI2CPR3Z44KAR0HG822 \n",
"4 Loan originated FHA-insured 254900YA1AQXNM8QVZ06 \n",
"\n",
" loan_amount interest_rate \n",
"0 264185000.0 NA \n",
"1 74755000.0 1.454 \n",
"2 66005000.0 NA \n",
"3 65005000.0 3.0 \n",
"4 63735000.0 2.99 "
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# what are the actions taken and types for those loans (show the text, not numbers)? Practice INNER JOIN.\n",
"pd.read_sql(\"\"\"\n",
"SELECT actions.action_taken, loan_types.loan_type, loans.lei, loans.loan_amount, loans.interest_rate\n",
"FROM loans\n",
"INNER JOIN actions ON loans.action_taken = actions.id\n",
"INNER JOIN loan_types ON loans.loan_type = loan_types.id\n",
"ORDER BY loan_amount DESC\n",
"LIMIT 5\n",
"\"\"\", conn)"
]
},
{
"cell_type": "code",
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
"id": "b7d4a687-70bf-46e7-a715-013977358d05",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>lei</th>\n",
" <th>action_taken</th>\n",
" <th>loan_type</th>\n",
" <th>loan_amount</th>\n",
" <th>interest_rate</th>\n",
" <th>loan_purpose</th>\n",
" <th>income</th>\n",
" <th>id</th>\n",
" <th>loan_purpose</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>3</td>\n",
" <td>Refinancing</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" lei action_taken loan_type loan_amount interest_rate loan_purpose income \\\n",
"0 None None None None None None None \n",
"\n",
" id loan_purpose \n",
"0 3 Refinancing "
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# what is a loan_purpose that doesn't appear in the loans table? Practice LEFT/RIGHT JOIN.\n",
"pd.read_sql(\"\"\"\n",
"SELECT *\n",
"FROM loans\n",
"RIGHT JOIN purposes ON loans.loan_purpose = purposes.id\n",
"WHERE loans.loan_purpose IS NULL\n",
"\"\"\", conn)"
]
},
{
"cell_type": "code",
"id": "fc73517c-cf57-4a91-bb9d-2fbfc76544d5",
"metadata": {},
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>COUNT(*)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>447367</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" COUNT(*)\n",
"0 447367"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"# how many rows are in the table? Practice COUNT(*).\n",
"pd.read_sql(\"\"\"\n",
"SELECT COUNT(*)\n",
"FROM loans\n",
"\"\"\", conn)"
"id": "e91feeee-8689-4f57-a991-f6ca3fee2a6d",
"metadata": {},
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>COUNT(income)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>399948</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" COUNT(income)\n",
"0 399948"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# how many non-null values are in the income column? Practice COUNT(column).\n",
"pd.read_sql(\"\"\"\n",
"SELECT COUNT(income)\n",
"FROM loans\n",
"\"\"\", conn)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "d688a1a3-3740-4dcf-8de5-b8f9f3e928b3",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id</th>\n",
" <th>loan_type</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>Conventional</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>FHA-insured</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>VA-guaranteed</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>FSA/RHS-guaranteed</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" id loan_type\n",
"0 1 Conventional\n",
"1 2 FHA-insured\n",
"2 3 VA-guaranteed\n",
"3 4 FSA/RHS-guaranteed"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"pd.read_sql(\"\"\"\n",
"SELECT *\n",
"FROM loan_types\n",
"\"\"\", conn)"
"id": "12333532-0618-4ed4-b71b-260a4f35e581",
"metadata": {},
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>AVG(interest_rate)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2.21657</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" AVG(interest_rate)\n",
"0 2.21657"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"# what is the average interest rate for loans of type \"Conventional\"? Practice AVG.\n",
"pd.read_sql(\"\"\"\n",
"SELECT AVG(interest_rate)\n",
"FROM loans\n",
"INNER JOIN loan_types ON loans.loan_type = loan_types.id\n",
"WHERE loan_types.loan_type = 'Conventional'\n",
"\"\"\", conn)"
"id": "23c00af3-e385-435a-8bf6-f5cfe64f02db",
"metadata": {},
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>loan_type</th>\n",
" <th>AVG(interest_rate)</th>\n",
" <th>COUNT(*)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Conventional</td>\n",
" <td>2.216570</td>\n",
" <td>389217</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>VA-guaranteed</td>\n",
" <td>1.919140</td>\n",
" <td>24551</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>FHA-insured</td>\n",
" <td>2.211670</td>\n",
" <td>30496</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>FSA/RHS-guaranteed</td>\n",
" <td>2.523942</td>\n",
" <td>3103</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" loan_type AVG(interest_rate) COUNT(*)\n",
"0 Conventional 2.216570 389217\n",
"1 VA-guaranteed 1.919140 24551\n",
"2 FHA-insured 2.211670 30496\n",
"3 FSA/RHS-guaranteed 2.523942 3103"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"# how many loans are there of each type? Practice GROUP BY.\n",
"pd.read_sql(\"\"\"\n",
"SELECT loan_types.loan_type, AVG(interest_rate), COUNT(*)\n",
"FROM loans\n",
"INNER JOIN loan_types ON loans.loan_type = loan_types.id\n",
"GROUP BY loan_types.loan_type\n",
"\"\"\", conn)"
"id": "2400a6a4-7056-47e0-b202-3bbb85a77b2f",
"metadata": {},
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>loan_type</th>\n",
" <th>AVG(interest_rate)</th>\n",
" <th>count</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Conventional</td>\n",
" <td>2.21657</td>\n",
" <td>389217</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>VA-guaranteed</td>\n",
" <td>1.91914</td>\n",
" <td>24551</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>FHA-insured</td>\n",
" <td>2.21167</td>\n",
" <td>30496</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" loan_type AVG(interest_rate) count\n",
"0 Conventional 2.21657 389217\n",
"1 VA-guaranteed 1.91914 24551\n",
"2 FHA-insured 2.21167 30496"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"# which loan types appear at least 10,000 times? Practice HAVING.\n",
"pd.read_sql(\"\"\"\n",
"SELECT loan_types.loan_type, AVG(interest_rate), COUNT(*) as count\n",
"FROM loans\n",
"INNER JOIN loan_types ON loans.loan_type = loan_types.id\n",
"GROUP BY loan_types.loan_type\n",
"HAVING count >= 10000\n",
"\"\"\", conn)"
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}