For running the SQL statements in Microsoft Access, see the previous chapter.
In principle, just copy and paste the SQL statement in the SQL View and Run.
The SQL commands and clauses are not case sensitive; in general, they are written in upper case.
19.3 ALL SQL Statements NEED EDITS
Code
library(DBI)library(duckdb)tblStudent=read.table(file ="tblStudent.txt", sep ="\t", header =TRUE)conn<-dbConnect(duckdb())duckdb_register(conn =conn, name ="tblStudent", value =tblStudent)# Note: We write the SQL statement in multiple lines This will be easy to read# for long SQL statements The following is equivalent to: sSQL = 'SELECT * FROM# tblStudent'sSQL="SELECT * FROM tblStudent"dbGetQuery(conn, sSQL)# Do not close connection if you need to run more SQL statements# dbDisconnect(conn, shutdown = TRUE)
19.4SELECT
Code
sSQL="SELECT * FROM tblStudent"sSQL="SELECT SName, Gender, Age FROM tblStudent"sSQL="SELECT SName AS StudentName, Gender, Age FROM tblStudent"
19.5WHERE
Code
sSQL="SELECT SName AS StudentName, Gender, AgeFROM tblStudentWHERE Age > 16 AND Attendance > 85"
19.6ORDER BY
Code
sSQL="SELECT SName AS StudentName, Gender, Age, AttendanceFROM tblStudentWHERE Age > 16 AND Attendance > 85ORDER BY Attendance ASC"
19.7SELECT with DISTINCT
Code
sSQL="SELECT COUNT(DISTINCT Age)FROM tblStudent"sSQL="SELECT DISTINCT ClassIDFROM tblStudentWHERE Age > 16 AND Attendance > 85ORDER BY Attendance ASC"
19.8SELECT with Aggregate function
Code
sSQL="SELECT COUNT(DISTINCT Age)FROM tblStudent"sSQL="SELECT COUNT(Age)FROM tblStudent"sSQL="SELECT COUNT(Age) AS NFROM tblStudentWHERE Attendance > 85"sSQL="SELECT COUNT(Age) AS NFROM tblStudent WHERE Attendance BETWEEN 85 AND 95"sSQL="SELECT COUNT(Age) AS N, AVG(Age) as AvgAgeFROM tblStudent WHERE Attendance BETWEEN 85 AND 95"sSQL="SELECT MIN(Age) AS Min, MAX(Age) AS Max, SUM(Age) AS Sum, AVG(Age) AS Avg, COUNT(Age) AS NFROM tblStudent ORDER BY N ASC"
19.9GROUP BY
Code
sSQL="SELECT Gender, COUNT(DISTINCT ClassID) AS NFROM tblStudent WHERE Attendance BETWEEN 85 AND 95GROUP BY GenderORDER BY Gender ASC"sSQL="SELECT Gender, ClassID, COUNT(DISTINCT ClassID) AS NFROM tblStudent WHERE Attendance BETWEEN 85 AND 95GROUP BY Gender, ClassIDORDER BY ClassID, Gender ASC"sSQL="SELECT Gender, COUNT(DISTINCT ClassID) AS N, AVG(Age) AS AvgAgeFROM tblStudent WHERE Attendance BETWEEN 85 AND 95GROUP BY GenderORDER BY N ASC"sSQL="SELECT Gender, COUNT(DISTINCT ClassID) AS N, MIN(Age) AS Min, MAX(Age) AS Max, SUM(Age) AS Sum, AVG(Age) AS AvgFROM tblStudent WHERE Attendance BETWEEN 85 AND 95GROUP BY GenderORDER BY N ASC"
19.10HAVING
Code
sSQL="SELECT Age, COUNT(ClassID) AS NFROM tblStudentWHERE Attendance > 85GROUP BY AgeHAVING N > 1"sSQL="SELECT Gender, COUNT(ClassID) AS N, AVG(Age) as AvgAgeFROM tblStudent WHERE Attendance BETWEEN 85 AND 95GROUP BY GenderHAVING AvgAge > 16ORDER BY N ASC"
LEFT JOIN: Everything on the LEFTAND those matches on the RIGHT
Code
sSQL="SELECT * FROM tblClassLEFT JOIN tblTeacher ON tblClass.TeacherID = tblTeacher.TeacherID"sSQL="SELECT * FROM tblClass AS CLEFT JOIN tblTeacher AS TON C.TeacherID = T.TeacherID"sSQL="SELECT tblClass.ClassID, tblClass.Subject, tblClass.TeacherID FROM tblClassLEFT JOIN tblTeacher AS TON tblClass.TeacherID = T.TeacherID"sSQL="SELECT C.ClassID, C.Subject, C.TeacherID FROM tblClass AS CLEFT JOIN tblTeacher AS TON C.TeacherID = T.TeacherID"sSQL="SELECT C.ClassID, C.Subject, C.TeacherID, T.TeacherID, T.TName, T.ExperienceFROM tblClass AS CLEFT JOIN tblTeacher AS TON C.TeacherID = T.TeacherID"sSQL="SELECT T.TeacherID, T.TName, T.Experience, C.ClassID, C.Subject, C.TeacherIDFROM tblTeacher AS TLEFT JOIN tblClass AS C ON T.TeacherID = C.TeacherID"
19.11.2 Anti LEFT JOIN
Anti LEFT JOIN or LEFT JOIN Exclusive
Everything on the LEFT that is NOT on the RIGHT
Code
sSQL="SELECT T.TeacherID, T.TName, T.Experience, C.ClassID, C.Subject, C.TeacherIDFROM tblTeacher AS TLEFT JOIN tblClass AS C ON T.TeacherID = C.TeacherIDWHERE C.TeacherID IS NULL"
19.11.3RIGHT JOIN
RIGHT JOIN: Everything on the RIGHTAND those matches on the LEFT
Code
sSQL="SELECT C.ClassID, C.Subject, S.StudentID, S.SName, S.ClassIDFROM tblClass AS C RIGHT JOIN tblStudent AS SON C.ClassID = S.ClassIDORDER BY S.StudentID"
19.11.4 Anti RIGHT JOIN
Anti RIGHT JOIN or RIGHT JOIN Exclusive
Everything on the RIGHT that is NOT on the LEFT
Code
sSQL="SELECT C.ClassID, C.Subject, S.StudentID, S.SName, S.ClassIDFROM tblClass AS C RIGHT JOIN tblStudent AS SON C.ClassID = S.ClassIDWHERE C.ClassID IS NULLORDER BY S.StudentID"
19.11.5INNER JOIN
INNER JOIN: Only those matches on the LEFTAND the RIGHT
Code
sSQL="SELECT S.StudentID, S.SName, S.ClassID, M.StudentID, M.MarksFROM tblStudent AS SINNER JOIN tblMarks AS MON S.StudentID = M.StudentIDORDER BY S.StudentID"
19.11.6FULL JOIN
FULL JOIN or OUTER JOIN: Everything on the LEFTAND everything on the RIGHT
Code
sSQL="SELECT S.StudentID, S.SName, S.ClassID, M.StudentID, M.MarksFROM tblStudent AS SFULL JOIN tblMarks AS MON S.StudentID = M.StudentIDORDER BY S.StudentID"