Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions sql-queries-12/Exclude_a_Column_in_SQL/MySQL.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Exclude textbook column from course table
SET @sql = NULL;
SELECT GROUP_CONCAT(CONCAT('`', COLUMN_NAME, '`') ORDER BY ORDINAL_POSITION SEPARATOR ', ')
INTO @sql
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'university'
AND TABLE_NAME = 'course'
AND COLUMN_NAME <> 'textbook';
SET @sql = CONCAT('SELECT ', @sql, ' FROM course');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
17 changes: 17 additions & 0 deletions sql-queries-12/Exclude_a_Column_in_SQL/PostgreSQL.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Excludes code column from department table
DO $$
DECLARE
sql_query text;
BEGIN
SELECT 'SELECT ' ||
string_agg(quote_ident(column_name), ', ' ORDER BY ordinal_position) ||
' FROM Department'
INTO sql_query
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'department'
AND column_name <> 'code';

RAISE NOTICE 'Generated SQL: %', sql_query;
EXECUTE sql_query;
END $$;
9 changes: 9 additions & 0 deletions sql-queries-12/Exclude_a_Column_in_SQL/SQL_Server.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Excludes gpa column from the student table
DECLARE @sql NVARCHAR(MAX);
SELECT @sql =
'SELECT ' + STRING_AGG(QUOTENAME(name), ', ')
+ ' FROM dbo.student'
FROM sys.columns
WHERE object_id = OBJECT_ID('dbo.student')
AND name <> 'gpa';
EXEC sp_executesql @sql;