Form a Chemical Bond - Problem

You are given a table Elements that contains information about chemical elements.

The table has the following structure:

  • symbol (varchar): Primary key representing the element symbol
  • type (enum): The type of element - 'Metal', 'Nonmetal', or 'Noble'
  • electrons (int): Number of electrons the element can give or needs

Rules:

  • If type is 'Noble', electrons is 0
  • If type is 'Metal', electrons is the number of electrons that one atom can give
  • If type is 'Nonmetal', electrons is the number of electrons that one atom needs

Chemical Bond Formation: Two elements can form a bond if one is 'Metal' and the other is 'Nonmetal'.

Write a SQL query to find all pairs of elements that can form a chemical bond. Return the result in any order.

Table Schema

Elements
Column Name Type Description
symbol PK varchar Element symbol (primary key)
type enum Element type: 'Metal', 'Nonmetal', or 'Noble'
electrons int Electrons given (Metal) or needed (Nonmetal), 0 for Noble
Primary Key: symbol
Note: Each row represents one chemical element with bonding properties

Input & Output

Example 1 — Basic Chemical Bonding
Input Table:
symbol type electrons
Na Metal 1
Cl Nonmetal 1
He Noble 0
Output:
metal nonmetal
Na Cl
💡 Note:

Sodium (Na) is a metal that can give 1 electron, and Chlorine (Cl) is a nonmetal that needs 1 electron. They can form an ionic bond. Helium (He) is a noble gas and cannot form bonds with other elements.

Example 2 — Multiple Bonding Possibilities
Input Table:
symbol type electrons
Li Metal 1
Ca Metal 2
F Nonmetal 1
O Nonmetal 2
Output:
metal nonmetal
Ca F
Ca O
Li F
Li O
💡 Note:

All metals (Li, Ca) can form bonds with all nonmetals (F, O), regardless of their electron counts. The result shows all possible Metal-Nonmetal pairs sorted alphabetically.

Example 3 — No Bonding Possible
Input Table:
symbol type electrons
Ne Noble 0
Ar Noble 0
Output:
metal nonmetal
💡 Note:

Only noble gases are present in the table. Since bonds can only form between metals and nonmetals, no chemical bonds are possible, resulting in an empty result set.

Constraints

  • 1 ≤ number of elements ≤ 100
  • type is one of 'Metal', 'Nonmetal', or 'Noble'
  • 0 ≤ electrons ≤ 8
  • symbol consists of 1-2 uppercase letters

Visualization

Tap to expand
Form a Chemical Bond - Problem OverviewInput: ElementssymboltypeelectronsNaMetal1ClNonmetal1HeNoble0Self-JoinMetal × NonmetalOutput: Chemical BondsmetalnonmetalNaClChemical Bond: Na⁺ + Cl⁻ → NaClNa⁺Cl⁻Ionic Bond
Understanding the Visualization
1
Input Table
Elements with their types and electron properties
2
Self-Join
Pair metals from first table with nonmetals from second
3
Output Pairs
All valid Metal-Nonmetal bond combinations
Key Takeaway
🎯 Key Insight: Use self-join when you need to find relationships between rows in the same table
Asked in
Google 15 Microsoft 12
23.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen