How to clear the database of unused fields?
You can get the fields that aren’t in use anywhere by running this SQL query:

SELECT f.*
FROM craft_fields f
LEFT JOIN craft_fieldlayoutfields l ON l.fieldId = f.id
WHERE l.id IS NULL


The easiest way to delete them would be from the Control Panel, but if you really want to limit your work to the database, you would need to delete those fields’ rows in craft_fields, and their corresponding column (“field_TheFieldHandle”) in craft_content. For Matrix fields, you would also need to drop the corresponding DB table (“craft_matrixcontent_TheFieldHandle”).