DISTINCT and LIMIT | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

DISTINCT and LIMIT

In tutorial SQL --> DISTINCT and LIMIT its confusing ; you start counting from 0 and look for the third record ,then the result is the forth record being first _can there be a mistake and 2nd record was to be first?

31st Jan 2017, 6:12 AM
Hamid
Hamid - avatar
4 Answers
+ 4
Starting from 0 index: 0 = record 1 1 = record 2 2 = record 3 3 = record 4
1st Feb 2017, 7:41 PM
Mark Foxx
Mark Foxx - avatar
0
For being able to do that, mysql must know what to do with the other columns. You GROUP BY the column that should be unique and use a function that will tell it what to do with the others (also-called aggregate function). MAX() and COUNT() are common examples: SELECT studentId, COUNT(courseId) AS AmountEnrolledCourses FROM student_enrollment GROUP BY studentId SELECT athlete, MAX(distance) AS PersonalRecord FROM longjump GROUP BY athlete
3rd Feb 2017, 1:01 PM
Akwin Lopez
Akwin Lopez - avatar
0
You need to use a subquery: select a.no, count(*) total from ( select no from test order by no desc limit 5 ) a group by a.no order by total desc;
3rd Feb 2017, 1:01 PM
Akwin Lopez
Akwin Lopez - avatar
0
Distinct is used to fetch unique records from table, whereas limit is used to fetch set of records starting from index zero.
4th Feb 2017, 8:15 PM
mahendra k
mahendra k - avatar