w3resource

SQL Challenges-1: Unique values

SQL Challenges-1: Exercise-14 with Solution

From the following table, write a SQL query to find the unique marks. Return the unique marks.

Input:

Table: student_test

Structure:

FieldTypeNullKeyDefaultExtra
student_idint(11)YES
marks_achievedint(11)YES

Data:

student_idmarks_achieved
156
274
315
474
589
656
793

Sample Solution:

SQL Code(MySQL):

DROP TABLE IF EXISTS student_test;
CREATE TABLE student_test(student_id int,  marks_achieved int);
INSERT INTO student_test VALUES (1, 56);
INSERT INTO student_test VALUES (2, 74);
INSERT INTO student_test VALUES (3, 15);
INSERT INTO student_test VALUES (4, 74);
INSERT INTO student_test VALUES (5, 89);
INSERT INTO student_test VALUES (6, 56);
INSERT INTO student_test VALUES (7, 93);
SELECT * FROM student_test;
SELECT DISTINCT(marks_achieved) AS  'Unique Marks' FROM student_test;

Sample Output:

Unique Marks|
------------|
          56|
          74|
          15|
          89|
          93|

SQL Code Editor:


Contribute your code and comments through Disqus.

Previous: Find the even or odd values.
Next: Find Student Supporter.



SQL: Tips of the Day

How to select the nth row in a SQL database table?

Basically, PostgreSQL and MySQL supports the non-standard:

SELECT...
LIMIT y OFFSET x 

Oracle, DB2 and MSSQL supports the standard windowing functions:

SELECT * FROM (
  SELECT
    ROW_NUMBER() OVER (ORDER BY key ASC) AS rownumber,
    columns
  FROM tablename
) AS foo
WHERE rownumber <= n

Database: SQL

Ref: https://bit.ly/3zPxcD8