Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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

SQL Server SELECT into existing table.

INSERT INTO dbo.TABLETWO
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:

INSERT INTO dbo.TABLETWO
  (col1, col2)
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

Database: SQL Server

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