Question

In: Computer Science

There is a table t with a column salary which is the primary key. Write a...

  1. There is a table t with a column salary which is the primary key. Write a query to get the nth minimum salary without using multi query or limit. You will be awarded no points if you use multi query or limit or rownum or rowid.


no subquery are allowed. please solve it with joins

Solutions

Expert Solution

Solution:

Creating a Table:

create table t(salary int primary key);

Inserting values into table:

insert into t values (10000),(20000),(30000),(11000),(19000),(90000);

Query:

select t.salary from t join t t1 where t1.salary <= t.salary group by t.salary having count(distinct t1.salary)=2;

Explanation:

In above query joined the table t with itself that means considered table 't' as two instances.since it contains only one column no need to mention any join condition . In group by clause grouping salary column in first table so that salary is compare with each and every salary in second table and counting in having clause where the count is equal to given n value.

In the query we can replace the n value with 2 so that we can get nth highest salary.it's better to put in a procedure and call the procedure with passing n value as parameter.

Here procedure code:

delimiter $$
create procedure nth_minimum_salary(in n int)
begin
select t.salary from t join t t1 where t1.salary <=t.salary group by t.salary having count(distinct t1.salary)=n;
end $$

Calling Procedure:

call nth_minimum_salary(3) $$

call nth_minimum_salary(2) $$

Code and Output Screenshots:

Note : if you have any queries please post a comment thanks a lot..always available to help you...


Related Solutions

create table node( node_id integer primary key, node_color varchar(10)); create table edge( edge_id integer primary key,...
create table node( node_id integer primary key, node_color varchar(10)); create table edge( edge_id integer primary key, origin_id integer, destination_id integer, foreign key (origin_id) references node(node_id), foreign key (destination_id) references node(node_id)); write an SQL query that lists all those nodes that have edges with a destination node that has color 'red'.
Create table, create primary and foreign key constraints. Create index on the table to satisfy a...
Create table, create primary and foreign key constraints. Create index on the table to satisfy a query with aggregate functions.
In the MedicalVisit table the primary key is defined as (Date, Doctor, Patient).     1. If the...
In the MedicalVisit table the primary key is defined as (Date, Doctor, Patient).     1. If the primary key were to be changed to (Doctor, Patient), how would it affect the design and information in the database?  (eg information structure in the database, normal form etc.) 2.  Please Draw an ER diagram based on the above original Relational Model
Conduct a test of hypothesis to determine whether the mean salary (Salary column) of the teams...
Conduct a test of hypothesis to determine whether the mean salary (Salary column) of the teams was different than $75.0 million. Use the 5% level of significance. Note: We do not know the populations standard deviation. Salary 93.6 143 108.7 61.7 95.2 67.1 103.9 71.4 189.6 79.4 106.5 24.1 68.3 89.1 66.2 87.3 99.7 68.9 54.4 30.5 87.8 108.5 71 115.2 89.4 38.5 58.1 90.2 90.3 37.3 Select one: a. p = 19.2%. Do not reject the null. The mean...
Which of the following best describes the primary key? Select one: a. It's the encryption key...
Which of the following best describes the primary key? Select one: a. It's the encryption key that gets exchanged first with another party. b. It's the password that needs to be entered to open the database. c. It's the first item that is entered into the database on a screen. d. It's a field that uniquely identifies the record.
Consider the following table definitions create table node( node_id integer primary key, node_color varchar(10)); create table...
Consider the following table definitions create table node( node_id integer primary key, node_color varchar(10)); create table edge( edge_id integer primary key, origin_id integer, destination_id integer, foreign key (origin_id) references node(node_id), foreign key (destination_id) references node(node_id)); What is the result of the following query? select node_id, node_color, destination_id from node, edge; An inner join of the tables node and edge that lists origin node_id and node_color together with the node_id of the destination node for all those nodes that have outgoing...
1. Use SQL to create a polyinstantiated table including a primary key and a unique constraint...
1. Use SQL to create a polyinstantiated table including a primary key and a unique constraint 2.Use SQL to insert multiple records for each security classification with the same ID. You must have 4 classifications. 3.Use SQL to create 4 schemas, one for each security classification 4.Use SQL to create a view in each schema that restricts the records to those belonging to a particular security classification and restricts the columns to only those columns that have relevant data. 5.Select...
what does a primary key and foreign key relates to? Which drive does a magnet damage...
what does a primary key and foreign key relates to? Which drive does a magnet damage the most what the Raid differences ? what a NIC does what is NAS and what's it used for and how it can be used in a network setting which device has a built in antenna what RF is used for (Radio Frequency )
Write down the initial simplex table for the following problem. Find the first pivot column and...
Write down the initial simplex table for the following problem. Find the first pivot column and the first pivot. Do not complete the simplex algorithm. A department store has up to 16000TL to spend on television advertising for sale. All ads will be placed with one television station. A 30-second ad costs 1000TL on daytime TV and is viewed by 14000 potential customers, 1800TL on prime-time TV and is viewed by 18000 potential customers, and 1500TL on late-night TV and...
what is super key,  candidate key, and primary key, and foreign key in terms of database? and...
what is super key,  candidate key, and primary key, and foreign key in terms of database? and plz provide some examples, thanks.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT