Query from Postgres to SQL Server I have this query in PostgreSQL and I am not familiar with PostgreSQL. I need to convert it to SQL Server.

SELECT (applicant_majors.*)::applicant_majors 
FROM applicant_majors 
WHERE applicant_majors.applicant_degree_id = (applicant_degree_1).id 
ORDER BY id 
LIMIT 1 OFFSET 1


This is the whole query 

           FROM (SELECT college_attendeds,
                    colleges,
                    applicant_degree_1,
                    applicant_degree_2,
                    (SELECT (applicant_majors.*)::applicant_majors FROM applicant_majors WHERE applicant_majors.applicant_degree_id = (applicant_degree_1).id ORDER BY id           LIMIT 1         ) AS applicant_degree_1_applicant_major_1,
                    (SELECT (applicant_majors.*)::applicant_majors FROM applicant_majors WHERE applicant_majors.applicant_degree_id = (applicant_degree_1).id ORDER BY id           LIMIT 1 OFFSET 1) AS applicant_degree_1_applicant_major_2,
                    (SELECT (applicant_minors.*)::applicant_minors FROM applicant_minors WHERE applicant_minors.applicant_degree_id = (applicant_degree_1).id ORDER BY minor_number LIMIT 1         ) AS applicant_degree_1_applicant_minor_1,
                    (SELECT (applicant_minors.*)::applicant_minors FROM applicant_minors WHERE applicant_minors.applicant_degree_id = (applicant_degree_1).id ORDER BY minor_number LIMIT 1 OFFSET 1) AS applicant_degree_1_applicant_minor_2,
                    (SELECT (applicant_majors.*)::applicant_majors FROM applicant_majors WHERE applicant_majors.applicant_degree_id = (applicant_degree_2).id ORDER BY id           LIMIT 1         ) AS applicant_degree_2_applicant_major_1,
                    (SELECT (applicant_majors.*)::applicant_majors FROM applicant_majors WHERE applicant_majors.applicant_degree_id = (applicant_degree_2).id ORDER BY id           LIMIT 1 OFFSET 1) AS applicant_degree_2_applicant_major_2,
                    (SELECT (applicant_minors.*)::applicant_minors FROM applicant_minors WHERE applicant_minors.applicant_degree_id = (applicant_degree_2).id ORDER BY minor_number LIMIT 1         ) AS applicant_degree_2_applicant_minor_1,
                    (SELECT (applicant_minors.*)::applicant_minors FROM applicant_minors WHERE applicant_minors.applicant_degree_id = (applicant_degree_2).id ORDER BY minor_number LIMIT 1 OFFSET 1) AS applicant_degree_2_applicant_minor_2
               FROM (SELECT (college_attendeds.*)::college_attendeds AS college_attendeds,(colleges.*)::college_bases AS colleges,
                            (SELECT (applicant_degrees.*)::applicant_degrees FROM applicant_degrees WHERE applicant_degrees.college_attended_id = college_attendeds.id ORDER BY id LIMIT 1         ) AS applicant_degree_1,
                            (SELECT (applicant_degrees.*)::applicant_degrees FROM applicant_degrees WHERE applicant_degrees.college_attended_id = college_attendeds.id ORDER BY id LIMIT 1 OFFSET 1) AS applicant_degree_2
                       FROM college_attendeds
            LEFT OUTER JOIN college_bases AS colleges ON (college_attendeds).college_id = colleges.id AND colleges.type = 'College') AS applicant_degree_data) AS college_attended_data
LEFT OUTER JOIN degree_bases AS applicant_degree_1_degree ON (applicant_degree_1).degree_id = applicant_degree_1_degree.id AND applicant_degree_1_degree.type IN ('Degree', 'PtDegree')
  LEFT OUTER JOIN degree_bases AS applicant_degree_2_degree ON (applicant_degree_2).degree_id = applicant_degree_2_degree.id AND applicant_degree_2_degree.type IN ('Degree', 'PtDegree');
applicant_degree_1 its a subquery that contains:

SELECT (applicant_degrees.*)::applicant_degrees FROM applicant_degrees     
WHERE applicant_degrees.college_attended_id = college_attendeds.id ORDER BY id LIMIT 1



  The query above try to get the greatest id from applicant_degrees for each college_attended_id.


Then, I beleive your query can run on MSSQL like this:

SELECT applicant_majors.*
FROM applicant_majors 
WHERE applicant_majors.applicant_degree_id IN (
    SELECT MAX(id) FROM applicant_degrees 
    WHERE applicant_degrees.college_attended_id = college_attendeds.id
)
ORDER BY id 
LIMIT 1 OFFSET 1



  Notice that I changed the ORDER BY and LIMIT for a MAX aggregate function.


Hope it helps.