0% found this document useful (0 votes)
34 views

Faq Queries Ans

The document contains examples of SQL queries to: 1) Find the second highest marks from a student table using a subquery to count the number of marks greater than or equal to each mark. 2) Count the total number of rows in a table using COUNT(*). 3) Eliminate duplicate records from the results using DISTINCT. 4) Insert a record into a table. 5) Display a row using its index value in the WHERE clause.

Uploaded by

preparation2011
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Faq Queries Ans

The document contains examples of SQL queries to: 1) Find the second highest marks from a student table using a subquery to count the number of marks greater than or equal to each mark. 2) Count the total number of rows in a table using COUNT(*). 3) Eliminate duplicate records from the results using DISTINCT. 4) Insert a record into a table. 5) Display a row using its index value in the WHERE clause.

Uploaded by

preparation2011
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

1)Find the nth max ? Find the second max marks in th class ?

Ans) SELECT s1.marks FROM student s1 WHERE 2=(SELECT COUNT(DISTINCT(s2.marks))


FROM student s2 WHERE s2.marks >= s1.marks) Hint:Whenever condition is true count is incremented. Explaination

S1.marks 100 550 500 150 300

S2.marks 100 550 500 150 300


i.e 100>=100yes..so count=1 550>=100.yes.so count=2 500>=100..yes.so count=3 150>=100yes.s0 count=4 300>=100yesso count=5

s2.marks >= s1.marks ?

So the final value of count becomes 5condition not satisfied(becoz we want 2 or 2nd max). Now, s2.marks >= s1.marks ? Is 100>=550noso count=0 550>=550yes..so count=1 500>=550..noso count=1

150>=550no..so count=1 300>=550noso count=1 So the final value of count becomes 1condition not satisfied(becoz we want 2 or 2nd max). Again, s2.marks >= s1.marks ? Is 100>=500no so count=0

550>=500yes..so count=1 500>=500..yes..so count=2 150>=500..no ..so count=2 300>=500..no..so count=2 So the final value of count becomes 2condition satisfied(becoz we want 2 or 2nd max). So s1.marks is 500

2) Write a query to find the total number of rows in a table


SELECT COUNT(*) AS total_num_rows_ FROM student

3) Write a query to eliminate duplicate records in the results of a table


SELECT DISTINCT * FROM student

4) Write a query to insert a record into a table 5) Write a query to display a row using index
SELECT * FROM student WHERE roll_no='9'

You might also like