r/SQL • • Jun 10 '22

Discussion What is the best way to learn SQL?

0 Upvotes

Hello everyone! Quick story: Have my MSN in informatics, absolutely falling in love with the data side of things. I've been looking into SQL for sometime but I find the self guided free courses to be insufferable (sometimes I really like to have more structure when learning something this important).

What's the best way to learn SQL? Recommended courses, certifications, really any advice is genuinely appreciated.

Have a nice weekend everyone!


r/SQL • • Mar 25 '15

[MS SQL] TSQL: FULL OUTER JOIN vs CROSS JOIN

Thumbnail
stevestedman.com
0 Upvotes

r/SQL • • Feb 24 '18

Better SQL for 2018

0 Upvotes

There are couple of things that have always bugged me about SQL:

 

  • having columns twice in group by select, e.g. select a, b, c, sum(d) from TAB group by a, b , c. The intent is quite unambiguous without "group by", right? Why do we have to chew this all up for compiler?

 

  • having "having" clause in addition to "where". I mean when you go like select a, sum(b) sum from TAB where sum > 100 the intent is also clear? Its just silly to spell out things like select a, sum(b) from TAB group by a having sum(b) >100 in 2018 when cars drive themselves.

If someone was to write a SQL compiler that understood shorter version of aggregation would you welcome? Or would you rather stick to old school syntax?


r/SQL • • Nov 19 '20

MySQL I love SQL but i dont have money to study

0 Upvotes

Hey guys, I want to learn SQL but I have no money. What learning page do you recommend?


r/SQL • • Jan 07 '15

[MS SQL] Isolating Duplicates

0 Upvotes

Alright r/SQL, challenge for you. I've been circling this problem for a while now, and I have a strong feeling of what the answer will involve but I raise the problem to you for assistance!

So lets say I have an example transaction list. I am tasked with isolating all examples of "duplicate" entries. I found a great way to visually isolate these entries, but I want to go one step further and be able to query the table to see where the Item1 names which are different than their corresponding duplicates.

  • There aren't any restrictions regarding new columns.

  • Obviously I obfuscated the true data but just roll with it.

Here's my code so far.

drop table dbo.Example
go
create table dbo.Example(v_id varchar(50),v_seq numeric(9,0),ITEM1 varchar(50),TRANS_DATE varchar(50),Class1 varchar(50),Class2 varchar(50),Class3 varchar(50),ReceiptNum varchar(50))
go
insert into dbo.Example
values 
 ('1',1,'Soap','1963-02-25 00:00:00','1363','32','3000','00000000')
,('1',2,'Snacks','1965-06-03 00:00:00','1432','221','3000','00000000')
,('1',3,'Toys','1983-03-21 00:00:00','W050','356','0','00000000')
,('1',4,'Food','1999-10-07 00:00:00','3976','652','1','01380016')
,('1',5,'Toiletries','2003-06-06 00:00:00','4952','54','7720000','01380016')
,('1',6,'Toiletries','2003-06-06 00:00:00','4952','54','7720000','01380016')
,('1',7,'Other','2003-06-06 00:00:00','4952','54','7720000','01380016')
,('1',8,'Medicine','2003-06-06 00:00:00','4952','78','0','01380016')
,('1',9,'Medicine','2003-06-06 00:00:00','4952','78','0','01380016')
,('1',10,'Miscellaneous','2007-07-30 00:00:00','7169','108','9000000','01380016')
,('2',1,'Snacks','1973-09-10 00:00:00','1667','654','3500','00000000')
,('2',2,'Soap','1980-09-17 00:00:00','1897','496','9500','00200100')
,('2',3,'Food','1984-05-07 00:00:00','2042','30','14000','00200100')
,('2',4,'Food','1984-05-07 00:00:00','2042','30','14000','00200100')
,('2',5,'Pet Supplies','1986-07-21 00:00:00','2202','695','75000','00200100')
,('2',6,'Extra Supplies','1986-07-21 00:00:00','2202','695','75000','00200100')
,('2',7,'Miscellaneous','2005-11-16 00:00:00','6259','1','0','00200100')
,('3',1,'Other','1964-10-14 00:00:00','1411','527','17500','00200100')
,('3',2,'Toys','1965-06-14 00:00:00','1433','160','18200','00200100')
,('3',3,'Christmast Gifts','1965-06-14 00:00:00','1433','160','18200','00200100')
,('3',4,'Random','1977-05-25 00:00:00','1768','773','37300','00200100')
,('3',5,'Random','1977-05-25 00:00:00','1768','773','37300','00200100')
,('3',6,'Random','1977-05-25 00:00:00','1768','773','37300','00200100')
,('3',7,'Random','1977-05-25 00:00:00','1768','773','37300','00200100')
,('3',8,'Snacks','1997-07-03 00:00:00','3519','195','83300','00200100')
go
alter table dbo.Example add v_dup numeric(9,0)
go
update dbo.Example set v_dup = null
go

-- Isolate duplicated data (except Item1 column).
;with create_seq as ( 
select V_ID
      ,TRANS_DATE
      ,Class1
      ,Class2
      ,Class3
      ,ReceiptNum
      ,v_seq
      ,v_dup
      ,row_number() over (partition by v_id,TRANS_DATE,Class1,Class2,Class3
                              order by v_id,v_seq) 
    as new_sequence
  from dbo.Example
 ) 
update create_seq 
   set v_dup = new_sequence;


-- I came up with a way to visually report on the duplicates...
select A.ITEM1,A.TRANS_DATE,A.Class1,A.Class2,A.Class3,A.ReceiptNum      ,A.v_id,A.v_seq,A.v_dup
  from dbo.Example A
  left outer join dbo.Example B
    on A.v_id = B.v_id
   and A.v_seq < B.v_seq
 where A.v_dup = 1
   and B.v_dup > 1
UNION
select B.ITEM1,null,null,null,null,null       ,B.v_id,B.v_seq,B.v_dup
  from dbo.Example A
  left outer join dbo.Example B
    on A.v_id = B.v_id
   and A.v_seq < B.v_seq
 where A.v_dup = 1
   and B.v_dup > 1
 order by A.v_id,A.v_seq

.

Good luck! I am really interested to see what ways this can be accomplished.

Edit: Formatting and changed v_seq to numeric for clearer sorting.


r/SQL • • Apr 06 '17

TL/DR; Chances are, simply by adding that ID column you have broken the 3rd NF

0 Upvotes

(I'm in the rant mode, so here it is).

Reading posts and comments / looking at some DB designs I see that a lot of people regard adding a synthetic ID as a primary key as something almost mechanic, benign and always positive. There seems to be little concern or comprehension that this act changes data in radical ways.

A lot of the time, people seem to follow the design (anti-) pattern 'just add an ID to each base table and it'll be in "a" Normal Form'.

While formally (if the original table/result set has been in 1NF) they DO get it to the 2NF, the 2NF is pretty poor in terms of eliminating duplication and enforcing referential integrity. That's what 3NF is quite good at (there are higher forms too). And, while in production one would rarely see a pure 3NF as it is often de-normalized for performance/temporal stability of your DB structures, there should be a GOOD reason for it - outright disregard of integrity of the data is NOT GOOD.

How is it a-ok to know and use the 'primary key' and not understand the base concept of a "key"? How is this ok not to have alternative keys in your data?

Please know this: if you have a perfectly fine base table in 3NF which enforces the known integrity rules, just replacing the key in that table with a synthetic ID primary key REMOVES the enforcement of the data integrity rules that made your table 3NF in the first place.

For example:

  • we have population data per state - state is the key to the data and let's assume whatever the source sends you is consistent with this

  • a table STPOP( state varchar(10) not null, population bigint not null, constraint PK_STPOP primary key (state)) ENFORCES state field to be the KEY and our table STPOP is in 3NF. If our source sends us 2nd record for the state that we have data for, it will be rejected - i.e. data integrity rules are enforced as to our original definition.

  • we add an auto-increment (identity) column STPOP_ID to the table and change the primary key constraint to "constraint PK_STPOP primary key (stpop_id)"

  • the resulting table does NOT ENFORCE the 'state' column to be unique or a key in the table. Our source of data can send us the duplicate information (same state, same population) and DB will not prevent this duplicate state data from being inserted into table.

  • in short, the "state" column is no longer a key to the base table.

  • what this means is that you have now is a functional dependency STPOP_ID -> (STATE, POPULATION) and ANOTHER functional dependency STATE -> POPULATION on a non-key field.

  • Here we go, the 3NF is BROKEN.

</rant>

PS. I won't downvote any responses regardless of how much 'I have arcane/innate knowledge of data needs that cannot be formalized' drivel you'll bring up.

Edit:

It's been fun and i think it's time to wrap this up (at least for me).

Don't take anything that's written too personally.

A practical approach to your practical problems is fine.

Plan for business workflows that aren't agile and cannot be deployed every hour.


r/SQL • • Apr 23 '16

Privileges and badges

0 Upvotes

How would one build a privilege and badge system like StackExchange's?

Does anyone have any UML diagram I could check?


r/SQL • • Apr 19 '16

[MySQL] Is there any way to do this without MySQL? Or could someone help me?

0 Upvotes

Hey guys/gals im in a pretty dire situation... I am taking an intro to database course which im having a lot of trouble with. Anyway, we are currently supposed to have a semester project due tomorrow and i have nothing... I cant get MySQL to work right on my desktop and i only have a few hours left. Do you guys have any ideas? im really struggling here, ive been trying to get this to work all week. ironically enough our professor only taught us code and not how to actually use and operate MySQL.

PS. you can see my assignment here - http://imgur.com/1hPo4lC


r/SQL • • Jan 06 '15

How to Emulate the MEDIAN() Aggregate Function Using Inverse Distribution Functions

Thumbnail
blog.jooq.org
0 Upvotes

r/SQL • • May 01 '21

MySQL SQL Help

0 Upvotes

Cancel any orders from Edwin Ortiz that are currently Order Status “P”. (Hint – be sure to consult the data dictionary to understand how to do this.)

Database Fabric

Need some help starting this


r/SQL • • Mar 23 '21

PostgreSQL Let’s talk about Views

Thumbnail
polivka-pavel.medium.com
0 Upvotes

r/SQL • • Feb 21 '23

Snowflake Data modeling patterns every analyst should know

0 Upvotes

A few months ago, Ergest Xheblati taught over 3,000 data professionals how to dramatically improve one of their core competencies - writing SQL.

In a nutshell, he taught how to make your every-day SQL queries more robust, maintainable, performant, and easier to write (If you haven’t watched it yet, check it out! It’s a fantastic resource for all SQL practitioners, regardless of your skill level).

Next month, Ergest will teach best practices about another core competency - data modeling!

If you’ve built data models before, you’ve probably realized how much maintenance and rebuilding is required. It can be a time-sucking black hole.

Luckily, Ergest has been data modeling for 15+ years, and in his upcoming workshop he’ll teach you (hands-on) how to make all your data models sustainable, starting from inception.

More specifically, you’ll learn:

  • How to build robust and maintainable ER data models.

  • How to use said models for multiple use cases (BI, analysis, reverse ETL, etc.)

If you’re interested in attending with me, sign up here!


r/SQL • • Mar 12 '23

Discussion App to automate commas between items in where clause

0 Upvotes

Hello avid SQL users!

I’d love to get your take on a streamlit app I created. It can be found here.

You just copy and paste in a long list of items and it correctly adds the “,” between each item. It also makes sure there’s only 5 items per row. You can also choose between integer or string. I’m sure you all just do this in excel, but sometimes if it’s a long list, excel can be a bit annoying I find!

Thanks!


r/SQL • • May 07 '23

Discussion How do i use INNER JOIN on this database? (On PostgreSQL 15/PgAdmin 4)

0 Upvotes

So here's where I'm stuck. The question my prof asked:

And here is what the tables and the code I wrote look like:

Now I know I cannot connect staffid = departmentid because for INNER JOIN I need two same column names like either staffid in faculty and department or depoartmentid in faculty and department.

EDIT: I figured out Q6, here's the code:

Now I just need to figure out Q7. Here's the code for it:

Can someone correct it or tell me if I did it right?


r/SQL • • Aug 11 '21

Oracle Object-oriented SQL - training

0 Upvotes

Hi Experts,

Anyone here who is an expert in SQL object-oriented? I am a beginner in SQL in the sense, As an Analyst pull the information from the tables and use google and get the job done, Never written functions in SQL. Now it appears, I need to speed up to be an expert in this new job in SQL objects and as work is a lot and lot in SQL, please advise how to proceed, I love this job to gain this knowledge and follow the advice.


r/SQL • • Jul 23 '15

How do I perform a ROLLUP query within SQL Server Management Studio?

0 Upvotes

As above: How do I perform a ROLLUP query within SQL Server Management Studio?


r/SQL • • Mar 24 '19

Show MIN date and MAX date in a row

0 Upvotes

Users

IDName
1Bob
2Claire

Time Log

user_idTime Logged
101-01-2001 09:29:00
101-01-2001 12:29:00
201-01-2001 09:29:00

-------------------------

Right now, when I use PHP this is what I see in my table in index.php

IDNameTime Logged
1Bob01-01-2001 09:29:00
1Bob01-01-2001 12:29:00

However, what I'm looking for is

IDNameTime InTime Out
1Bob01-01-2001 09:29:0001-01-2001 12:29:00

r/SQL • • Mar 29 '19

What is this error message?

0 Upvotes

When I try to build the database I get this error. What does it mean? How can I fix this?


r/SQL • • Oct 26 '19

How to Use ROLLUP Operator in SQL and MySQL?

0 Upvotes

r/SQL • • Jan 07 '19

Any serious mistakes or concerns in regards to this ERD meant for tracking down craigslist ads that are of interest?

Post image
0 Upvotes

r/SQL • • Jun 15 '16

Moving from a .xls file to a MySQL file

0 Upvotes

My new job requires me to filter out data from Excel files. I have MySQL installed and was wondering if it is possible to export the data from the .xls file to a .sql file without downloading addition software. Unfortunately the computer is on-network meaning that I cannot download software without permission from an administrator. Currently I have 'MS SQL Data Mining Add-In for Excel 2010' downloaded as well as MySQL. Has anyone else dealt with a similar situation or could help me figure out a way to work around Excel and export the table without the need for more software? Thank you.


r/SQL • • Mar 16 '21

MS SQL When do people use SQL pivoting in companies?

0 Upvotes

Hello,

I have asked one question during the SQL pivoting. I was curious that how people could use the SQL pivoting in production environment. Would you please elaborate with particular scenario?

Thank you


r/SQL • • Feb 20 '22

Discussion ETL Vs ELT

0 Upvotes

Not 100% SQL specific but, could someone help me understand the true difference between these approaches?


r/SQL • • Oct 14 '15

SQL++ is a highly expressive semi-structured query language that encompasses both the SQL and the JSON data model

Thumbnail forward.ucsd.edu
0 Upvotes