##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:
Non-bromine atoms refers to element != 'br'; connections refer to bonds connected to an atom

##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: What is the average number of connections per non-bromine atom across molecules that have bonds or multiple atoms, and how many of these non-bromine atoms have more than 2 connections?
Evidence: Non-bromine atoms refers to element != 'br'; connections refer to bonds connected to an atom
Answer: Sorry, I cannot answer.
Permission: denied (Missing column permissions: connected: atom_id, bond_id)
Gold SQL: WITH BromineCount AS (
    SELECT 
        molecule_id,
        SUM(CASE WHEN element = 'br' THEN 1 ELSE 0 END) AS bromine_atoms,
        COUNT(*) AS total_atoms
    FROM 
        atom
    GROUP BY 
        molecule_id
),
MoleculeBonds AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(DISTINCT b.bond_id) AS bond_count
    FROM 
        molecule m
    LEFT JOIN 
        bond b ON m.molecule_id = b.molecule_id
    GROUP BY 
        m.molecule_id, m.label
),
AtomConnections AS (
    SELECT 
        a.atom_id,
        a.molecule_id,
        a.element,
        COUNT(c.bond_id) AS connection_count
    FROM 
        atom a
    LEFT JOIN 
        connected c ON a.atom_id = c.atom_id
    GROUP BY 
        a.atom_id, a.molecule_id, a.element
)
SELECT 
    COUNT(*) AS non_bromine_atoms,
    SUM(CASE WHEN ac.connection_count > 2 THEN 1 ELSE 0 END) AS highly_connected_non_bromine_atoms,
    COUNT(DISTINCT ac.molecule_id) AS molecules_with_non_bromine_atoms,
    ROUND(AVG(ac.connection_count), 2) AS avg_connections_per_non_bromine_atom
FROM 
    AtomConnections ac
JOIN 
    MoleculeBonds mb ON ac.molecule_id = mb.molecule_id
JOIN 
    BromineCount bc ON ac.molecule_id = bc.molecule_id
WHERE 
    ac.element <> 'br'
    AND (mb.bond_count > 0 OR bc.total_atoms > 1)