You can find homework assignments that are submitted as forks for a MOOC Github repo. In this case, you'll find multiple SQL statements for the same question. The SQL style will be a little biased because all the students are following the same course.
An example:
Then find forks of the main repo.
The files contain code like this:
-- TASK 09
-- *Write a T-SQL script that shows for each
-- town a list of all employees that live in it.
GO
USE TelerikAcademy
GO
DECLARE townCursor CURSOR READ_ONLY FOR
SELECT t.Name, t.TownID
FROM Towns t
OPEN townCursor
DECLARE @townName nvarchar(50), @townID int
FETCH NEXT FROM townCursor INTO @townName, @townID
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE empCursor CURSOR READ_ONLY FOR
SELECT e.FirstName, e.LastName, a.TownID
FROM Employees e, Addresses a
WHERE e.AddressID = a.AddressID
OPEN empCursor
DECLARE @empFirstName nvarchar(50), @empLastName nvarchar(50), @empTownID nvarchar(50), @toPrint nvarchar(1000) = @townName + ' ->'
FETCH NEXT FROM empCursor INTO @empFirstName, @empLastName, @empTownID
WHILE @@FETCH_STATUS = 0
BEGIN
IF(@empTownID = @townID)
BEGIN
SET @toPrint = @toPrint + ' ' + @empFirstName + ' ' + @empLastName + ','
END
FETCH NEXT FROM empCursor INTO @empFirstName, @empLastName, @empTownID
END
DECLARE @length int = LEN(@toPrint)
SET @toPrint = SUBSTRING(@toPrint, 1, @length - 1)
PRINT @toPrint;
CLOSE empCursor
DEALLOCATE empCursor
FETCH NEXT FROM townCursor INTO @townName, @townID
END
CLOSE townCursor
DEALLOCATE townCursor
This MOOC is just one example...
Location
column on the table, there was a LocationId that was a foreign key back to a Locations table? The query changes toselect e.name from employees as e join locations as l on e.locationid = l.locationid where l.name = 'LA'
– Ben Thul Feb 26 '15 at 21:41