MySQL Tricky Interview Questions - Part 1 | Must-Know SQL Questions

Other Blogs

Blogs ❯❯ SQL

Image could not load

Interview Questions by Van Tay Media on Unsplash

MySQL interview देना है? तो सिर्फ basic questions से आगे बढ़ना जरूरी है! आज के competitive job market में, tricky और advanced MySQL questions आपके interview को next level पर ले जा सकते हैं।

इस blog में हम discuss करेंगे कुछ ऐसे MySQL के tricky interview questions जो अक्सर interviewers पूछते हैं, ताकि आप confidently अपने next interview में perform कर सकें।

1. Duplicate Rows को कैसे Delete करें ?

Question : अगर table में दो या ज़्यादा rows बिल्कुल identical/same हैं, तो आप सिर्फ एक row कैसे delete करेंगे?

Answer : इस situation में simple `WHERE` clause काम नहीं करेगा क्योंकि सारी rows identical हैं। आपको `ROWID` या `LIMIT` का use करना पड़ेगा। MySQL में आप `DELETE` के साथ `LIMIT` use कर सकते हैं।

Identify duplicate rows

SELECT column1, column2, COUNT(*) FROM your_table GROUP BY column1, column2 HAVING COUNT(*) > 1;

Delete duplicate rows

WITH cte AS ( SELECT id, column1, column2, ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY id) AS row_num FROM your_table ) DELETE FROM your_table WHERE id IN (SELECT id FROM cte WHERE row_num > 1);

2. MySQL में NULL Values को कैसे Handle करते हैं?

Question : NULL values के साथ comparison कैसे करते हैं? = operator काम करेगा?

Answer : NULL को compare करने के लिए इस NULL या इस NOT NULL use करना चाहिए। = operator NULL के लिए काम नहीं करता क्योंकि NULL का मतलब unknown होता है।

SELECT * FROM employees WHERE manager_id IS NULL; or SELECT * FROM employees WHERE manager_id IS NOT NULL;

3. Aggregate Functions में NULL Values कैसे Treat होती हैं?

Answer : Aggregate functions जैसे SUM(), COUNT(), AVG() NULL values को ignore करते हैं। लेकिन COUNT(*) सब rows count करता है, चाहे NULL हो या न हो।

इसलिए हमेशा एक particular column को count करें जैसे primary id.

4. Indexes कैसे काम करते हैं? और Composite Index क्या है?

Answer : Indexes data को fast access करने के लिए use होते हैं। Composite index multiple columns पर बनाया जाता है जो queries को optimize करता है जब multiple columns filter या join में use हो रहे हो।

5. Transaction Isolation Levels क्या हैं?

Answer : Transaction isolation levels define करते हैं कि एक transaction दुसरे transaction के changes को कब तक देख सकता है।

Common levels हैं -

  • READ UNCOMMITTED

  • READ COMMITTED

  • REPEATABLE READ (MySQL का default)

  • SERIALIZABLE

6. Find Second Highest Salary Without Using LIMIT?

SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);

7. MySQL में Charset, Encoding, और Collation क्या है ?

MySQL में Charset, Encoding, और Collation 3 important concepts हैं जो text data को store करने और retrieve करने के लिए use किये जाते हैं।

1. Charset (Character Set)

Character Set (Charset) एक collection होता है characters का, जिसमे हर character का एक unique numeric value (code point) होता है। मतलब, एक charset एक collection of characters को define करता है, जो आप अपने database में store कर सकते हो।

Example : utf8, latin1, utf8mb4

UTF-8 एक popular character set है, जिसमे दुनिया के सारे languages के characters का representation हो सकता हैं जैसे English, Hindi, Arabic, Chinese, etc.।

2. Encoding

Encoding का मतलब है एक specific character को binary data (bytes) में convert करना, ताकि उससे store किया जा सके। हर charset का अपना encoding method होता है, जिसमे character को bytes के form में convert किया जाता है।

UTF-8 encoding हर character को 1 से 4 bytes तक में encode करता है।

Encoding और Character Set का relation -
  • Charset character set को represent करता है।

  • Encoding एक particular character set के characters को bytes में convert करने का process है।

Example :

  • अगर आप UTF-8 charset use कर रहे हो, तो UTF-8 encoding use करके characters को 1 से 4 bytes तक encode किया जायगा।

  • latin1 charset के case में, हर character 1 byte में encode होता है।

3. Collation

Collation एक set of rules होता है जो determine करता है कि characters को compare और sort कैसे किया जायगा। Collation का use query results को order करते वक्त होता है और text comparison में भी होता है।

Collation case sensitivity और accent sensitivity को define करता है। For example, क्या 'a' और 'A' को same treat किया जायेगा या अलग?

Collation में sorting rules भी होते हैं, जैसे किसी specific language के characters को किस order में arrange करना है।

Types of Collation:

  • Case Sensitive : utf8_bin — 'a' और 'A' को अलग characters treat किया जाता है।

  • Case Insensitive : utf8_general_ci — 'a' और 'A' को same characters treat किया जाता है। 

  • CI का मतलब है Case Insensitive 

  • BIN का मतलब है Binary, मतलब case-sensitive और exact match.

Example :

  • utf8_general_ci : ये collation general case-insensitive comparison allow करता है. तो 'a' और 'A' को same consider किया जायेगा।

  • utf8_unicode_ci : ये collation Unicode characters को compare करता है, जो ज़्यादा accurate और language-specific sorting provide करता है।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    1 Comment(s) found !