Code
SELECT * FROM tblStudent
PROC SQL
All examples in this chapter are presented using PROC SQL
For running the SQL statements in SAS, see the previous chapter.
In principle, just copy and paste the SQL statement in the PROC SQL command.
Note: No quote marks are required to enclose the SQL statement, but the semicolon at the end of the SQL statement is required. This is SAS-specific.
The SQL commands and clauses are not case sensitive; in general, they are written in upper case.
# Give your own path
libname SASData 'Path\To\Folder\data';
proc sql;
SELECT *
FROM tblStudent
;
quit;
%put NUMBER_RECORDS = &sqlobs;
SELECT
tblStudent
SELECT * FROM tblStudentSName, Gender, Age columns from tblStudent
SELECT SName, Gender, Age FROM tblStudentSName, Gender, Age columns from tblStudent
SName as StudentName
SELECT SName AS StudentName, Gender, Age FROM tblStudentSName, Gender, Age columns from tblStudent
SName as StudentName
SELECT SName AS StudentName, Gender, Age FROM tblStudent LIMIT 5WHERE
SName, Gender, Age columns from tblStudent
SName as StudentName
Age > 16
SELECT SName AS StudentName, Gender, Age
FROM tblStudent
WHERE Age > 16SName, Gender, Age columns from tblStudent
SName as StudentName
Age > 16 AND Attendance greater than 85%
SELECT SName AS StudentName, Gender, Age
FROM tblStudent
WHERE Age > 16 AND Attendance > 85SName, Gender, Age columns from tblStudent
SName as StudentName
Age > 16 AND Attendance BETWEEN 85% and 95%
SELECT SName AS StudentName, Gender, Age
FROM tblStudent
WHERE Age > 16 AND Attendance BETWEEN 85 AND 90SName, Gender, Age columns from tblStudent
SName as StudentName
Age values include 15 and 16 years
SELECT SName AS StudentName, Gender, Age
FROM tblStudent
WHERE Age IN (15, 16)SName, Gender, Age columns from tblStudent
SName as StudentName
StudentName starts with Al
SELECT SName AS StudentName, Gender, Age
FROM tblStudent
WHERE StudentName LIKE 'Al%'SName, Gender, Age columns from tblStudent
SName as StudentName
StudentName ends with an e
SELECT SName AS StudentName, Gender, Age
FROM tblStudent
WHERE StudentName LIKE '%e'SName, Gender, Age columns from tblStudent
SName as StudentName
Attendance is greater than any attendance from ClassID 1
SELECT SName AS StudentName, Gender, Age, Attendance
FROM tblStudent
WHERE Attendance > ANY (SELECT Attendance FROM tblStudent WHERE ClassID=1)ORDER BY
SName, Gender, Age columns from tblStudent
SName as StudentName
Age is greater than 16 years and Attendance is greater than 80%
SELECT SName AS StudentName, Gender, Age, Attendance
FROM tblStudent
WHERE Age > 16 AND Attendance > 80
ORDER BY Attendance ASCSName, Gender, Age columns from tblStudent
SName as StudentName
Age is greater than 16 years and Attendance is greater than 80%
SELECT SName AS StudentName, Gender, Age, Attendance
FROM tblStudent
WHERE Age > 16 AND Attendance > 80
ORDER BY Attendance DESC
LIMIT 2SELECT with Aggregate functionstblStudent, find all unique ClassID
SELECT DISTINCT ClassID
FROM tblStudenttblStudent, find total number of unique ClassID
SELECT COUNT(DISTINCT ClassID)
FROM tblStudenttblStudent, find all unique Age values in the data
SELECT DISTINCT Age
FROM tblStudenttblStudent, find total number of unique Age values in the data
SELECT COUNT(DISTINCT Age)
FROM tblStudenttblStudent, find total number of students with Attendance greater than 85%
SELECT COUNT(StudentID) AS N
FROM tblStudent
WHERE Attendance > 85tblStudent, find total number of students where Attendance between 85% and 95%
SELECT COUNT(StudentID) AS N
FROM tblStudent
WHERE Attendance BETWEEN 85 AND 95tblStudent, find total number of students and their average Age where Attendance between 85% and 95%
SELECT COUNT(StudentID) AS N, AVG(Age) as AvgAge
FROM tblStudent
WHERE Attendance BETWEEN 85 AND 95tblStudent, find minimum, maximum, sum, average of Age and total number of students
SELECT MIN(Age) AS Min, MAX(Age) AS Max,
SUM(Age) AS Sum, AVG(Age) AS Avg, COUNT(Age) AS N
FROM tblStudent GROUP BY
tblStudent, find total number of students in each class
SELECT ClassID, COUNT(StudentID) AS N
FROM tblStudent
GROUP BY ClassIDtblStudent, find total number of students in each class where Attendance was between 85% and 95%
SELECT ClassID, COUNT(StudentID) AS N
FROM tblStudent
WHERE Attendance BETWEEN 85 AND 95
GROUP BY ClassIDtblStudent, find total number of male and female students where Attendance between 85% and 95%
SELECT Gender, COUNT(StudentID) AS N
FROM tblStudent
WHERE Attendance BETWEEN 85 AND 95
GROUP BY GenderFrom tblStudent, find total number of male and female students by age where Attendance was between 85% and 95%
Get the records in ascending order by Gender and Age
SELECT Gender, Age, COUNT(StudentID) AS N
FROM tblStudent
WHERE Attendance BETWEEN 85 AND 95
GROUP BY Gender, Age
ORDER BY Gender, Age ASCFrom tblStudent, find total number of male and female students and their average age where Attendance was between 85% and 95%
Get the records in ascending order by Gender
SELECT Gender, COUNT(StudentID) AS N, AVG(Age) AS AvgAge
FROM tblStudent
WHERE Attendance BETWEEN 85 AND 95
GROUP BY Gender
ORDER BY Gender ASCFrom tblStudent, find minimum, maximum, sum, average of Age and total number of students where Attendance was between 85% and 95%
Get the records in ascending order by Gender
SELECT Gender,
COUNT(StudentID) AS N,
MIN(Age) AS Min, MAX(Age) AS Max,
SUM(Age) AS Sum, AVG(Age) AS Avg
FROM tblStudent
WHERE Attendance BETWEEN 85 AND 95
GROUP BY Gender
ORDER BY Gender ASCHAVING
From tblStudent, find total number of male and female students where Attendance greater than 85%
Present records where the count is greater than 3
SELECT Gender, COUNT(StudentID) AS N
FROM tblStudent
WHERE Attendance > 85
GROUP BY Gender
SELECT Gender, COUNT(StudentID) AS N
FROM tblStudent
WHERE Attendance > 85
GROUP BY Gender
HAVING N > 3From tblStudent, find total number of male and female students where Attendance between 85% and 95%
Present records where the average age is greater than 16 years
SELECT Gender, COUNT(ClassID) AS N, AVG(Age) as AvgAge
FROM tblStudent
WHERE Attendance BETWEEN 85 AND 95
GROUP BY Gender
SELECT Gender, COUNT(ClassID) AS N, AVG(Age) as AvgAge
FROM tblStudent
WHERE Attendance BETWEEN 85 AND 95
GROUP BY Gender
HAVING AvgAge > 16JOIN
Read all tables as data.frame
Register the connection with duckdb
Remember to provide separate table names for each table while registering it
The following script assumes that all data are in your current working directory
tblClass = read.table(file='tblClass.txt', sep='\t', header=TRUE)
tblTeacher = read.table(file='tblTeacher.txt', sep='\t', header=TRUE)
tblStudent = read.table(file='tblStudent.txt', sep='\t', header=TRUE)
tblMarks = read.table(file='tblMarks.txt', sep='\t', header=TRUE)
conn = dbConnect(duckdb())
duckdb_register(conn = conn, name = 'tblClass', df = tblClass)
duckdb_register(conn = conn, name = 'tblTeacher', df = tblTeacher)
duckdb_register(conn = conn, name = 'tblStudent', df = tblStudent)
duckdb_register(conn = conn, name = 'tblMarks', df = tblMarks)LEFT JOIN
LEFT JOIN: Everything on the LEFT AND those matches on the RIGHT

SELECT *
FROM tblClass
LEFT JOIN tblTeacher
ON tblClass.TeacherID = tblTeacher.TeacherID
SELECT *
FROM tblClass AS C
LEFT JOIN tblTeacher AS T
ON C.TeacherID = T.TeacherID
SELECT tblClass.ClassID, tblClass.Subject, tblClass.TeacherID
FROM tblClass
LEFT JOIN tblTeacher AS T
ON tblClass.TeacherID = T.TeacherID
SELECT C.ClassID, C.Subject, C.TeacherID
FROM tblClass AS C
LEFT JOIN tblTeacher AS T
ON C.TeacherID = T.TeacherID
SELECT C.ClassID, C.Subject, C.TeacherID,
T.TeacherID, T.TName, T.Experience
FROM tblClass AS C
LEFT JOIN tblTeacher AS T
ON C.TeacherID = T.TeacherID
SELECT T.TeacherID, T.TName, T.Experience,
C.ClassID, C.Subject, C.TeacherID
FROM tblTeacher AS T
LEFT JOIN tblClass AS C
ON T.TeacherID = C.TeacherIDLEFT JOIN
Anti LEFT JOIN or LEFT JOIN Exclusive
Everything on the LEFT that is NOT on the RIGHT

SELECT T.TeacherID, T.TName, T.Experience,
C.ClassID, C.Subject, C.TeacherID
FROM tblTeacher AS T
LEFT JOIN tblClass AS C
ON T.TeacherID = C.TeacherID
WHERE C.TeacherID IS NULLRIGHT JOIN
RIGHT JOIN: Everything on the RIGHT AND those matches on the LEFT

SELECT C.ClassID, C.Subject,
S.StudentID, S.SName, S.ClassID
FROM tblClass AS C
RIGHT JOIN tblStudent AS S
ON C.ClassID = S.ClassID
ORDER BY S.StudentIDRIGHT JOIN
Anti RIGHT JOIN or RIGHT JOIN Exclusive
Everything on the RIGHT that is NOT on the LEFT

SELECT C.ClassID, C.Subject,
S.StudentID, S.SName, S.ClassID
FROM tblClass AS C
RIGHT JOIN tblStudent AS S
ON C.ClassID = S.ClassID
WHERE C.ClassID IS NULL
ORDER BY S.StudentIDINNER JOIN
LEFT AND the RIGHT

SELECT S.StudentID, S.SName, S.ClassID,
M.StudentID, M.Marks
FROM tblStudent AS S
INNER JOIN tblMarks AS M
ON S.StudentID = M.StudentID
ORDER BY S.StudentIDFULL JOIN
FULL JOIN or OUTER JOIN: Everything on the LEFT AND everything on the RIGHT

SELECT S.StudentID, S.SName, S.ClassID,
M.StudentID, M.Marks
FROM tblStudent AS S
FULL JOIN tblMarks AS M
ON S.StudentID = M.StudentID
ORDER BY S.StudentID