SQL Interview Question:
You are given two unrelated tables:
- Product
— containing product details
- ProductSubcategory
— containing subcategory details
There are no common columns, and no foreign key or
primary key relationships between them.
Task:
Write a SQL query to perform a join between these two tables, despite having no
direct relationship.
Query And Related Table:
The problem with the above script is when you have exact match between the "product description" column and "subname" column then this code will work, but let say you have difference like Gadget and Gadgets then in this case the query will fail.
Feel free to post you comments over this solution, my approach would be:
1) Do the match over the letter by letter then count the total letter matched and then total letters if the percentage for this is above 80% then this record must be in the join condition. Basically perform the lookup with the help of SQL code.
2) Make abridge table with possible word combination and assign a unique identifier to the matched and then same identifier with mapped with the product and subcategory table. This approach looks fine but with the real time combination it doesn't feel feasible.
FAQ About SQL Interview Question:
1) What are the 5 basic SQL commands?
DDL,DML,DQL are three basic command component, which includes, Create, Alter, Truncate and Drop Command for DDL.
Insert, Update and Delete command in DML.
Select, From, Where, Group By, Having, Order By and Top clause in DQL Command.
2) How do I prepare for an SQL interview?
SQL is basically a language which allows user to interact with DB, so first of all one must have depth understanding about why DB is needed. Once this picture is clear now, we can move forward to understand basic SQL commands, JOINS, Sub-Queries, Cte's, Views, SP's, UDF followed by indexing and optimization.
Watching videos and reading blogs obviously will not help here, we must need to practice daily.
3) How to get 1 to 10 numbers in SQL?
To generate sequence number we can use Ranking function, basically row_number() if the sequence needed in the table column.
We can also use identity property associated with one columns only per table which will allow automatic sequence number when a new record get inserted.
Finally if the requirement is in select statement then recursive cte's can be used or while loop can ne used to do the same.
Conclusion:
So basically this requirement will allow to learn the complex joins scenarios, joining two tables directly on Primary key and Foreign Key relationship won't work every time, when optimization and DB redundancy need to be controlled at high level. In such case scenarios you must know joining tables on columns where part of data can be joined.
Comments
Post a Comment