Database Administrators
Q&A for database professionals who wish to improve their database skills
Latest Questions
-2
votes
1
answers
171
views
Why should I write SQL code with uppercased keywords?
Almost any literature writes SQL keywords and functions in *UPPERCASE*. e.g. ```sql SELECT column_1, MAX(column_b), * FROM table AS alias; ``` * What is the reason for this? * Is it because of a better readability? * Is it considered bad habit to write code in lowercase? e.g. ```sql select column_1,...
Almost any literature writes SQL keywords and functions in *UPPERCASE*. e.g.
SELECT column_1, MAX(column_b), * FROM table AS alias;
* What is the reason for this?
* Is it because of a better readability?
* Is it considered bad habit to write code in lowercase? e.g.
select column_1, max(column_b), * from table as alias;
What coding style should I use for SQL and why?
mrkskwsnck
(97 rep)
Apr 25, 2024, 11:00 AM
• Last activity: Apr 26, 2024, 05:09 AM
2
votes
1
answers
233
views
Common Table Expression syntax
Considering a query with CTEs: ```sql WITH cte_0 AS ( SELECT SUM(col_0) FROM table_0 WHERE condition_00 AND condition_01 ), cte_1 AS ( SELECT SUM(col_1) FROM table_0 WHERE condition_10 AND condition_11 ) SELECT ( ((SELECT * FROM cte_0) * 100) / (SELECT * FROM cte_1), ((SELECT * FROM cte_0) + 100) *...
Considering a query with CTEs:
WITH cte_0 AS (
SELECT SUM(col_0)
FROM table_0
WHERE condition_00
AND condition_01
),
cte_1 AS (
SELECT SUM(col_1)
FROM table_0
WHERE condition_10
AND condition_11
)
SELECT (
((SELECT * FROM cte_0) * 100) / (SELECT * FROM cte_1),
((SELECT * FROM cte_0) + 100) * (SELECT * FORM cte_1)
) FROM cte_0, cte_1
Would it be correct to leave the last FROM
out? And do I get a tuple of two numbers with a query like that? Or do I need a new table from which I SELECT *
to get two numbers?
I just started my first PostgreSQL project and I've seen different styles of writing it. All comments on the style are warmly welcomed. I have not found any suitable online PostgreSQL platform for testing queries. Are there any?
Question edited to include conditions and a more complicated result.
jvkloc
(133 rep)
Dec 3, 2023, 01:10 PM
• Last activity: Dec 5, 2023, 06:41 AM
1
votes
2
answers
57
views
Name of SQL programming style where KEYWORDS | CRITERIA are justified to centering line
Is there a name for this kind of SQL programming style? --Courtesy of Markus Winand of modern-sql.com/ SELECT * FROM (SELECT cities.* , ROW_NUMBER() OVER(PARTITION BY country ORDER BY population DESC) rn , COUNT(*) OVER(PARTITION BY country) ct_cities FROM cities ) t WHERE t.rn = 1 AND t.ct_cities >...
Is there a name for this kind of SQL programming style?
--Courtesy of Markus Winand of modern-sql.com/
SELECT *
FROM (SELECT cities.*
, ROW_NUMBER() OVER(PARTITION BY country ORDER BY population DESC) rn
, COUNT(*) OVER(PARTITION BY country) ct_cities
FROM cities
) t
WHERE t.rn = 1
AND t.ct_cities > 1
Source: [dbfiddle](https://dbfiddle.uk/DK5X59PV) related to this topic: Oracle Idea: FIRST() and LAST() aggregate functions
---------------
It's as if there is a line between the SQL keywords (right-justified) and the criteria (left-justified).
Whereas in other SQL styles, the SQL keywords are left-justified.
-----------------
I ask because that style seems easy to read and I want to find out more about it.
Edit; a related post: SQL Developer - Keep analytic function on single line when auto-formatting

User1974
(1527 rep)
Mar 16, 2023, 08:08 PM
• Last activity: Mar 17, 2023, 02:26 PM
15
votes
5
answers
16994
views
Does it make sense to use SQL Server's bracket notation in hand written code?
Code generators tend to be simpler when they generate output using the new Microsoft bracket notation (`[]`) for nearly everything. When I first saw it, I though wow a reincarnation of the somewhat banned quoted identifier notation. As far as I know it is a proprietary extension from Microsoft (mean...
Code generators tend to be simpler when they generate output using the new Microsoft bracket notation (
[]
) for nearly everything.
When I first saw it, I though wow a reincarnation of the somewhat banned quoted identifier notation.
As far as I know it is a proprietary extension from Microsoft (meaning Oracle doesn't support it).
Looking at SQL Server there is no difference if you define a table like
CREATE TABLE [dbo].[Table_2] ([col1] [int], [col2] [int]);
or
CREATE TABLE dbo.Table_2 (col1 int, col2 int);
Its a matter of personal or corporate style. Be consistent.
Now if you want to migrate your database to Oracle, brackets are no option.
You can use the old quoted identifiers, but these are case sensitive which causes a lot of trouble.
Is it a good idea to remove all brackets from generated code, avoid using blanks, other special characters and reserved keywords for names and just code in a way that most DBMS understand?
bernd_k
(12389 rep)
Jan 5, 2011, 02:18 PM
• Last activity: May 13, 2020, 05:39 PM
5
votes
2
answers
262
views
Are there other sites forbidding SQL developer to use c style comments?
My personal opinion is that C style comments should not be used by sql developers. The problem with C Style comments is, that they do not nest. C Style comments are one of the lifesavers for the support when they have to do some ad hoc corrections in stored procedures, but that only works well, when...
My personal opinion is that C style comments should not be used by sql developers.
The problem with C Style comments is, that they do not nest.
C Style comments are one of the lifesavers for the support when they have to do some ad hoc corrections in stored procedures, but that only works well, when they don't have to fight with existing C comments.
bernd_k
(12389 rep)
Mar 12, 2011, 09:19 AM
• Last activity: Oct 20, 2011, 08:09 PM
8
votes
2
answers
1154
views
Is this a new common pattern in Oracle Where exists ( Select NULL FROM...)?
Years ago, it was common to write where exists (Select * from some_table where some_condition) Last year I noticed that many t-sql scripts switched to using the number 1 instead of the star where exists (Select 1 from some_table where some_condition) just on [SO I saw this Oracle example][1] WHERE E...
Years ago, it was common to write
where exists (Select * from some_table where some_condition)
Last year I noticed that many t-sql scripts switched to using the number 1 instead of the star
where exists (Select 1 from some_table where some_condition)
just on SO I saw this Oracle example
WHERE EXISTS (SELECT NULL FROM ...
Is this a common pattern with Oracle?
And which are the performance arguments to use something like this.
bernd_k
(12389 rep)
Jan 5, 2011, 04:11 PM
• Last activity: Jan 7, 2011, 07:13 PM
Showing page 1 of 6 total questions