##Instruction:
Database: toxicology
Total Tables: 4
Total Columns: 11

Table Schemas:

Database: toxicology
Total Tables: 4
Total Columns: 11

Table Schemas:

Table: atom (3 columns)
-----------------------
• atom_id (text): the unique id of atoms
• molecule_id (text): identifying the molecule to which the atom belongs
  Values: commonsense evidence:
TRXXX_i represents ith atom of molecule TRXXX
• element (text): the element of the toxicology
  Values:  cl: chlorine
 c: carbon
 h: hydrogen
 o: oxygen
 s: sulfur
 n: nitrogen
 p: phosphorus
 na:...

Table: bond (3 columns)
-----------------------
• bond_id (text): unique id representing bonds
  Values: TRxxx_A1_A2:
TRXXX refers to which molecule
A1 and A2 refers to which atom
• molecule_id (text): identifying the molecule in which the bond appears
• bond_type (text): type of the bond
  Values: commonsense evidence:
-: single bond
'=': double bond
'#': triple bond

Table: connected (3 columns)
----------------------------
• atom_id (text): id of the first atom
• atom_id2 (text): id of the second atom
• bond_id (text): bond id representing bond between two atoms

Table: molecule (2 columns)
---------------------------
• molecule_id (text): unique id of molecule
  Values: "+" --> this molecule / compound is carcinogenic
'-' this molecule is not / compound carcinogenic
• label (text): whether this molecule is carcinogenic or not

##Evidence:
Hydrogen atoms refer to element = 'h'; single bonds refer to bond_type = '-'; double bonds refer to bond_type = '='

##Role Access Policy (Column-Level):
Role: MolecularDataEntry
Accessible Columns: molecule: molecule_id, label; atom: atom_id, molecule_id, element; bond: bond_id, molecule_id, bond_type

Based on the above schema and your role's column-level access policy, generate a SQL query to answer the question. If your role does not have access to the required columns, respond with: "Sorry, I cannot answer."


Question: For the label with the most hydrogen atoms, what are the total number of hydrogen atoms, average hydrogen atoms per molecule, average bonds per molecule, total single bonds, and total double bonds?
Evidence: Hydrogen atoms refer to element = 'h'; single bonds refer to bond_type = '-'; double bonds refer to bond_type = '='
Answer: WITH hydrogen_molecules AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(a.atom_id) AS hydrogen_count
    FROM 
        molecule m
    JOIN 
        atom a ON m.molecule_id = a.molecule_id
    WHERE 
        a.element = 'h'
    GROUP BY 
        m.molecule_id, m.label
),
molecule_bond_stats AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(DISTINCT b.bond_id) AS bond_count,
        SUM(CASE WHEN b.bond_type = '-' THEN 1 ELSE 0 END) AS single_bonds,
        SUM(CASE WHEN b.bond_type = '=' THEN 1 ELSE 0 END) AS double_bonds
    FROM 
        molecule m
    LEFT JOIN 
        bond b ON m.molecule_id = b.molecule_id
    GROUP BY 
        m.molecule_id, m.label
),
ranked_labels AS (
    SELECT 
        h.label,
        SUM(h.hydrogen_count) AS total_hydrogens,
        AVG(h.hydrogen_count) AS avg_hydrogens_per_molecule,
        AVG(mbs.bond_count) AS avg_bonds_per_molecule,
        SUM(mbs.single_bonds) AS total_single_bonds,
        SUM(mbs.double_bonds) AS total_double_bonds,
        RANK() OVER (ORDER BY SUM(h.hydrogen_count) DESC) AS hydrogen_rank
    FROM 
        hydrogen_molecules h
    JOIN 
        molecule_bond_stats mbs ON h.molecule_id = mbs.molecule_id
    GROUP BY 
        h.label
)
SELECT 
    label,
    total_hydrogens,
    avg_hydrogens_per_molecule,
    avg_bonds_per_molecule,
    total_single_bonds,
    total_double_bonds
FROM 
    ranked_labels
WHERE 
    hydrogen_rank = 1
ORDER BY 
    label ASC
LIMIT 1;
Permission: allowed (All required columns are permitted)
Gold SQL: WITH hydrogen_molecules AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(a.atom_id) AS hydrogen_count
    FROM 
        molecule m
    JOIN 
        atom a ON m.molecule_id = a.molecule_id
    WHERE 
        a.element = 'h'
    GROUP BY 
        m.molecule_id, m.label
),
molecule_bond_stats AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(DISTINCT b.bond_id) AS bond_count,
        SUM(CASE WHEN b.bond_type = '-' THEN 1 ELSE 0 END) AS single_bonds,
        SUM(CASE WHEN b.bond_type = '=' THEN 1 ELSE 0 END) AS double_bonds
    FROM 
        molecule m
    LEFT JOIN 
        bond b ON m.molecule_id = b.molecule_id
    GROUP BY 
        m.molecule_id, m.label
),
ranked_labels AS (
    SELECT 
        h.label,
        SUM(h.hydrogen_count) AS total_hydrogens,
        AVG(h.hydrogen_count) AS avg_hydrogens_per_molecule,
        AVG(mbs.bond_count) AS avg_bonds_per_molecule,
        SUM(mbs.single_bonds) AS total_single_bonds,
        SUM(mbs.double_bonds) AS total_double_bonds,
        RANK() OVER (ORDER BY SUM(h.hydrogen_count) DESC) AS hydrogen_rank
    FROM 
        hydrogen_molecules h
    JOIN 
        molecule_bond_stats mbs ON h.molecule_id = mbs.molecule_id
    GROUP BY 
        h.label
)
SELECT 
    label,
    total_hydrogens,
    avg_hydrogens_per_molecule,
    avg_bonds_per_molecule,
    total_single_bonds,
    total_double_bonds
FROM 
    ranked_labels
WHERE 
    hydrogen_rank = 1
ORDER BY 
    label ASC
LIMIT 1;