In this lab, I will follow my National University of Singapore (NUS) course: "Machine Learning and Data Analytics using Python - December 2023" and shows how to use basic SQL queries in DB Browser (SQLite).
Lab 4.1: Adding Calculations to queries
Count: The COUNT() function returns the number of rows that matches a specified criterion.
SELECTcount(LastName) AS [NameCount]FROMcustomersWHERELastName like 'B%';
Lab 4.2: String concatenation
-- Lab 4.2.1SELECTFirstName,LastName,FirstName || ' ' || LastName AS 'FULL Name'FROMcustomersWHERECountry = 'USA';-- Lab 4.2.2SELECTFirstName || ' ' || LastName || ' ' || Address || ', ' || City || ', ' || State || ' ' || PostalCode AS 'Mailling Address'FROMcustomersWHERECountry = 'USA';
Lab 4.3: String truncation
length: The LENGTH() function returns the length of a string (in bytes).
substr: The SUBSTR() function extracts a substring from a string (starting at any position).
-- Lab 4.3.1SELECTPostalCode,length(PostalCode) AS [length of Postal Code]FROMcustomersWHERECountry = 'USA';-- Lab 4.3.2SELECTPostalCode,substr(PostalCode, 1, 5) AS [Five Digit Postal Code]FROMcustomersWHERECountry = 'USA';
Lab 4.4: Additional string function
upper: The UPPER() function converts a string to upper-case.
lower: The LOWER() function converts a string to lower-case.
SELECTFirstName AS [First Name Unmodified],upper(FirstName) AS [First Name in Uppercase],lower(FirstName) AS [First Name in Lowercase],upper(FirstName) || ' ' || upper (LastName) AS [Full Name in Uppercase]FROMcustomers;
Lab 4.5: Data function
strftime: The strftime() function is used to format a datetime value based on a specified format.
SELECTstrftime('The Year is: %Y, The Month is: %m, The Day is: %d', '2011-05-22') AS [Text with conversion specifications];SELECTLastName,FirstName,strftime('%Y-%m-%d', BirthDate) AS [Birthday NO Timecode],strftime('%Y-%m-%d', 'now') AS [Now],strftime('%Y-%m-%d', 'now') - strftime('%Y-%m-%d', BirthDate) AS [Age]FROMemployeesORDER BYAge;
Lab 4.6: Round() function
round: The ROUND() function rounds a number to a specified number of decimal places.
SELECTavg (total) AS AveSales,round (AVG(total), 2) AS [Rounder Average Sales]FROMinvoices;
Lab 4.7: Round() function
round: The ROUND() function rounds a number to a specified number of decimal places.
SELECTavg (total) AS AveSales,round (AVG(total), 2) AS [Rounder Average Sales]FROMinvoices;
Lab 4.8: GROUP BY clause
GROUP BY: The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".
-- Lab 4.8.1SELECTBillingCity,avg (Total)FROMinvoicesGROUP BYBillingCityORDER BYBillingCity;-- Lab 4.8.2SELECTBillingCity,avg (Total)FROMinvoicesWHEREBillingCity like 'L%'GROUP BYBillingCityORDER BYBillingCity;-- Lab 4.8.3SELECTBillingCity,avg (Total)FROMinvoicesGROUP BYBillingCityHAVING -- cannot use whereAVG (Total) > 5ORDER BYBillingCity;-- Lab 4.8.4SELECTBillingCity,avg (Total)FROMinvoicesWHEREBillingCity like 'B%'GROUP BYBillingCityHAVING -- cannot use whereAVG (Total) > 5ORDER BYBillingCity;-- Lab 4.8.5SELECTBillingCountry,BillingCity,avg (Total)FROMinvoicesGROUP BYBillingCity,BillingCountryORDER BYBillingCountry;
Reference:
1. SQLite Home Page
2. SQL 教程
最初發表 / 最後更新: 2024-02-15 / 2024-02-15
0 comments:
張貼留言