SQL Flashcards
Why is a primary key used ?
- Maintain primary key integrity - like unique, not null
2. Faster lookup due to index
Why is foreign key used ?
to maintain referential integrity
What is the diff. b/w INTERSECT and inner join
Both will give matching rows, but intersect will remove duplicates ,
so intersect and join with unique/distinct are the same
What is the diff. b/w inner join and join
no diff
What is diff b/w union and union all
union all doesn’t remove duplicates
Both show all rows in both tables
Diff b/w cartesian product and union ?
cartesian shows m*n rows, union all shows m+n rows , union shows distinct (m+n) rows
Diff b/w truncate and delete ?
truncate is a DDL. Doesn’t need commit hence doesn’t need transaction log, thus much faster.
Any DML needs transaction log as it can be rolled back
What is a trigger?
Simple procedure which runs automatically on an event.
For eg. there might be an insert/update/delete event and a trigger based on it.
What is normalization?
Data is not duplicated in multiple tables, instead foreign keys are used to maintain referential integrity
Used to prevent insert/update/delete anomalies.
Diff b/w clustered and non-clustered index ?
Each table can only have 1 clustered index, multiple non-clustered indices.
Clustered index is physically stored on the database FS, so there can only be one. Usually with the PK.
Write an SQL query to find the position of the alphabet (‘a’) in the first name column ‘Amitabh’ from Worker table.:
Select INSTR(FIRST_NAME, ‘a’) from Worker where FIRST_NAME = ‘Amitabh’
What if i need to combine 2 columns ?
Select CONCAT(FIRST_NAME, ‘ ‘, LAST_NAME) AS ‘COMPLETE_NAME’ fromWorker;
How do i search all log entries after current time?
Depends on database.
but in sql server , can use > getdate() or current_timestamp etc.
What is an ER diagram?
Entity Relationship diagram which shows cardinality or relationship like 1:m 1:1 or 1:m
If cardinality is 1:m which table will have the foreign key ?
If cardinality is m:m, which table wil have the foreign key?
1: m - m table will have FKs of 1 table
m: m - association table which will have FKs from both tables