implemented SQL version of