The Queries in this page solved at the library database. It is a sample database generated by randomly records. You can download it from the link. You will see more than 500 examples in the future. You can filter from the search panel above by selecting sql statements. I hope you will like it

SQL Alias Examples

1-) List all students by mergin their name and surname

Solution 1)

Select name + surname from students

Solution 2)

Select name + surname as ns from students

Solution 3)

Select name + ' ' + surname as ns from students


ETİKETLER

Select - Alias -
2-) List the genders and student count of each gender

Solution 1)

Select gender,count(*) as StudentCount 
from students 
group by gender


ETİKETLER

Select - Group By - Aggragate - Alias -
3-) List the numbers of boys and girls in each class.

Solution 1)

Select sinif,gender,count(*) as StudentCount 
from students 
group by gender,sinif


ETİKETLER

Select - Group By - Aggragate - Alias -
4-) List only the number of female students in each class.

Solution 1)

Select sinif,gender,count(*) as StudentCount 
from students 
where gender = 'F'
group by gender,sinif


ETİKETLER

Select - Where - Group By - Aggragate - Alias -
5-) List the class names and number of students which the number of students more than 30.

Solution 1)

Select sinif,count(*) as StudentCount 
from students 
group by sinif
having count(*) >= 30


ETİKETLER

Select - Group By - Having - Alias -
6-) List the name and surname of the students and the number of books they read.

Solution 1)

Select name,surname,count(*) BookCount 
from students,borrows 
where students.studentId = borrows.studentId 
group by students.studentId,name,surname

Solution 2)

Select name,surname,count(*) BookCount 
from students
join borrows on students.studentId = borrows.studentId 
group by students.studentId,name,surname


ETİKETLER

Select - Join - Where - Group By - Aggragate - Alias - Çoklu Tablo -
7-) List the name and surname of the students and the number of books they read sorted by BookCount.

Solution 1)

Select name,surname,count(*) BookCount 
from students
join borrows on students.studentId = borrows.studentId 
group by students.studentId,name,surname
order by BookCount desc

Solution 2)

Select name,surname,count(*) BookCount 
from students,borrows 
where students.studentId = borrows.studentId 
group by students.studentId,name,surname
order by BookCount desc


ETİKETLER

Select - Join - Where - Group By - Order By - Aggragate - Alias - Çoklu Tablo -
8-) List the student count gruped by birthdate quarter

Solution 1)

Select Year(birthdate) as Year, datepart(qq,birthdate) as Quarter,count(*) as Count 
from students 
group by Year(birthdate),datepart(qq,birthdate)


ETİKETLER

Select - Group By - SQL Functions - Alias -
Library Database
Database