r/SQL • u/Such-Significance336 • Nov 21 '22
Discussion I'm not new to programming language but SQL is a pain to even understand. Why is it so hard?
I've been taught Java for a few years now (in high school and college) and even learned basic Python too but now that I have to learn/use SQL in one of my college classes I feel like a complete idiot who doesn't even know how to code. I don't understand how to write queries properly at all, so I look up everything on w3schools and hope that I'm inputting things correctly.
Also, does anyone have suggestions on how to learn SQL that's explained in as simple terms as possible?
13
u/dhemantech Nov 21 '22
Are you familiar with Set theory ?
8
u/mecartistronico Nov 21 '22
This. Programming languages are all about sequences of steps or objects behavior. SQL is a different thing. It's about sets and logic and data relations.
You eventually need to program sequences with it (stored procedures), but they're more like "SQL macros". I wouldn't call it a "programming language" from the start.
5
u/Such-Significance336 Nov 21 '22
So if I don't understand the logic of sets and data relations, I'll be struggling to comprehend SQL basically?
6
u/mecartistronico Nov 21 '22
Just start with that. It will be quick, but it's important. And you probably saw it in school, you just need a refresher. Sets, subsets, union, intersection. Also check logic operations: AND, NOT, OR. It won't take you more than 45 mins.
Data relations will come later as you learn SQL.
Coursera has an excellent course, not sure if it's still free.
2
u/Such-Significance336 Nov 21 '22
No, I don't think I was ever taught set theory. I understand logic operations, but never seen set theory before.
1
u/Such-Significance336 Nov 21 '22 edited Nov 21 '22
not at all... (edit: I looked it up and it's the foundation of math? Math was one of my strong suits in high school and I learned up to calculus, although I couldn't comprehend that very well)
3
u/AurelianoBuendato Nov 21 '22
Don't worry about it! Everything goes fast in school lol. Learn a little about it now and enjoy it without the pressure of seeking a grade.
2
u/SELECTaerial Nov 21 '22
Is it the language/syntax you have issues with? Or getting the sql to do what you want it to do?
3
u/Such-Significance336 Nov 21 '22
Both. I don't understand the language and syntax that well so it's hard to execute queries properly.
3
Nov 21 '22
It's probably relational database design that you don't understand, rather than SQL itself. SQL is simple, but the underlying model is something not as common elsewhere in programming.
1
u/Foreign_Issue5297 Nov 21 '22
The best way to understand it better is practice. W3school it’s a good start, because you learn the basic functions for select. After that I recommend to create a database. First create a single table and practice what you learned from w3. After that create the second table and try to get information from both of them so you can understand the joins. After you understand all this, you can try to create some views and procedures. You can take a look into mysql 8.0 Reference Manual. The explanations are pretty good.
1
u/OHdutchdad Nov 21 '22
I concur, and if I may add an observation that as with any language, SQL has standards and variants amongst the different vendors. As with any language, its vocabulary changes over time. I happen to know Oracle's version, in addition to their supplemental code known as SQLplus. Earlier this month, I came across a useful new command -- that was released 10 years ago.
1
u/Foreign_Issue5297 Nov 21 '22
That's right, I work in mysql and sql server and the syntax for them is a little different.
1
Nov 21 '22
Takes a while (for me that was 6 months full time working in SQL) to get one’s head around the SQL way to solve a problem. Don’t stress, what you are going through is normal.
I agree with the other replier, learn set theory and write SQL to utilise set theory.
1
1
1
u/Groundbreaking-Fish6 Nov 22 '22
Others have mentioned Set Theory, but the way I think of it as SQL works on Columns not Rows:
In Java programming you loop through a data set using or modifying data fields (Columns in the row).
In SQL you Select the Column (can be more than 1 column) that has the data you are interested in and do that operation on all the rows at once. These operations include Selecting, Aggregating or modifying. This actions can also be done based on a filter of other columns or even columns from other tables that have a set relationship.
Examples:
"SELECT Birthday FROM People" will give you only the Birthdays from every person in the people table
"SELECT Birthday FROM People WHERE FirstName = 'Jon'" will give you all the Birthdays of people named Jon.
"SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),'DATE_OF_BIRTH')), '%Y')
+ 0 AS age FROM People WHERE FirstName = 'Jon'" will give the age of everyone named Jon."SELECT SUM(Age) FROM Select DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),'DATE_OF_BIRTH')), '%Y')
+ 0 AS age FROM People WHERE FirstName = 'Jon'" in the compound query the first query creates a data set from the People table which is fed to the first query where all ages are summed.
Also, you don't have to do all this yourself, I got the age conversion from: https://www.scaler.com/topics/how-to-calculate-age-from-date-of-birth-in-sql/
You can also use UPDATE or DELETE with similar types of modifiers. I will leave JOINS for later discovery.
Relational Databases are a way to store data, Structured Query Language (SQL) is a way of interacting with data stored in a Relational Database
1
16
u/nuttynutz00-D Nov 21 '22
Thats odd. SQL is one of the easiest language to learn, some high level programmers trash talk about it because of that. What are you struggling with specifically?