Learn with best explanation

Coding tutorials and solutions

Java Core Interview Questions

Frequently asked basic java questions

SQL Basic Interview Questions

SQL Keys

SQL Differences

Job oriented frequently asked SQL keys Interview questions and answers with example

Here we have provided most common sql questions and answers that are frequently asked in interviews in software industries. Questions listed here are 100% job oriented and repeatedly asked. We have designed questions in sets so that it also helps build sql concept.

What is super key ?

A super key is a single key or combination of multiple keys which identify record in table. A Super key can contain additional attributes that are not needed for unique identification.

Student Table
student_id student_name phone address DOB
1 Waseem 03331111111 C-2, DHA Phase 5, Karachi 1993-10-09
2 Aamir 03001111111 1-34 DHA Phase 7, Karachi 1991-12-07
3 Iqra 03021111111 220, Block 17, Gulshan e Iqbal, Karachi 1995-09-07

In the above table column student_id is single super key that uniquely identifies record. For additional attribute student_id along with student_name also form super key. However student_name cannot uniquely identify record because there may be more then one students with the same name therefore it does so long with student_id.

What is candidate key ?

A candidate key is the key which is always unique and is never null. It is minimal set of super key. When there is super key without extra attributes it may be called candidate key. In example above from Student Table student_id and phone are candidate keys. Any candidate key can identify record from table.

What is primary key ?

Primary key is always unique and is never null. It is most appropriate candidate key make either student_id or phone as primary key for identification of record.

What is foreign key ?

Foreign key is responsible to create association or relationship between two tables. A foreign key in a table is always primary key in an other table. In the subject table bellowstudent_id_fk is foreign key which is referencing to student_id of Student Table

Subject Table
department_id student_id_fk department_name
1 1 DLD
2 3 Java
3 3 C++

What is composite key ?

A composite key is a key which does not guarantee its uniqueness. It can not uniquely identify record independently. Two or more attributes together form a composite key and uniquely identify the record. In Example below student_id does not uniquely identify record it does so in together with subject_id.

Subject Marks Table
student_id subject_id marks
1 1 63
2 1 55
1 2 70
3 2 80

What are secondary/alternate keys ?

These are key which does not guarantee uniqueness and can never be selected as primary keys. Attributes like marks, student_name, DOB, department_name and address are secondary/alternate keys.