Interview Questions: Database
- A cross join merges two tables on every record in a geometric fashion, everyrecord of one table is combined with every record from the other table. Two tables of 10records each in a cross join will create a table of 100 (10 X 10) records.
- An Inner join is used to match two tables based on values of a common field.An inner join gets data from both tables where the specified data exists in both tables. For E.g.List of students in the database that were absent on December 4, 2003, you would use aninner join between the two tables "Student" and "Attendance"SELECT Student.ID, Student.FirstName, Student.LastName, Attendance.AttendanceCode,Attendance.MinutesAbsent FROM Student INNER JOIN Attendance ONStudent.ID=Attendance.ID WHERE Attendance.AttendanceDate=’12/4/2003’
- An outer join gets data from the source table at all times, and returns data fromthe outer joined table ONLY if it matches the criteria. When using outer joins, fields will be setto NULL if data does not exist in the outer-joined table. For E.g.SELECT Student.ID, Student.FirstName, Student.LastName, Attendance.AttendanceCode,Attendance.MinutesAbsent FROM Student INNER JOIN StudentSchedule ONStudentSchedule.ID=Student.ID< br /> LEFT OUTER JOIN Attendance ONStudent.ID=Attendance.ID AND Attendance.AttendanceDate=’12/4/2003’ WHEREStudentSchedule.Course=’E NGLISH 9’
a)Left Outer join
- All outer joins retrieve records from both tables, just as an inner joindoes. However, an outer join retrieves all of the records from one of the tables. A column inthe result is NULL if the corresponding input table did not contain a matching record.
b)Right Outer join
- The right outer join is similar to the left outer join in that it retrieves allthe records from one side of the relationship, but this time it’s the right table. Only recordswhere the condition values match are retrieved from the left.
c)Full Outer Join
- The full outer join retrieves all records from both the left and the righttable.
4. Question: For ~n~ number of SQL SELECT statements connected by UNION, howmany times should we specify UNION to eliminate the duplicate rows?
Answer: Only Once
5. Question: In the WHERE clause what is BETWEEN and IN?
Answer: IN keyword helps to limit the selection criteria to one or more discrete values, theBETWEEN keyword allows for selecting a range. The syntax for the BETWEEN clause is asfollows:SELECT "column_name" FROM "table_name" WHERE "column_name" BETWEEN ~value1~AND ~value2~This will select all rows whose column has a value between ~value1~ and ~value2~.The syntax for using the IN keyword is as follows:SELECT "column_name"FROM "table_name"WHERE "column_name" IN (~value1~, ~value2~, ...)The number of values in the parenthesis can be one or more, with each values separated bycomma. Values can be numerical or characters. If there is only one value inside the