0d: 00h: 00m: 00s

🎁 Get 3 extra months of learning — completely FREE

That's 90 extra days to master new skills, build more projects, and land your dream job.

Become a PRO
Background Image

🎁 Get 3 extra months of learning completely FREE

Become a PRO

Ask Programiz - Human Support for Coding Beginners

Explore questions from fellow beginners answered by our human experts. Have some doubts about coding fundamentals? Enroll in our course and get personalized help right within your lessons.

  • All
  • Python
  • C
  • Java
  • CPP
  • SQL
  • JS
  • HTML
  • CSS
Abhay Jajodia
Expert
last week
Abhay Jajodia answered

Hello Omar, really nice question.

The logic behind SELECT in SQL is actually pretty simple once you see what problem it solves. A database table is like a giant spreadsheet. Most of the time, you don’t need every single piece of information in that table — you just need certain columns or certain rows.

SELECT is how you tell the database: “Show me only the parts I care about.”

For example, if a table has ten different columns but you only want to see age and country, you can write:

SELECT age, country
FROM Customers;

Now the database gives you just those two columns instead of the whole table. It’s efficient, it’s cleaner, and it makes it easier to work with your data.

So the real logic is:
Ask for the specific data you want instead of taking everything.

If you want to dig deeper into filtering, sorting, or selecting rows, I’m happy to help you explore that next.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
last week
Abhay Jajodia answered

Hello Omar, really nice question.

Using * in SQL grabs every column in the table. It works, but it often gives you more data than you actually need. When you switch to something like:

SELECT age
FROM Customers;

you’re telling the database exactly what you want. That makes your query faster, easier to understand, and avoids pulling in extra or sensitive information by accident.

So the idea is simple:
Only ask for the columns you actually need.
It keeps the query clean and efficient.

If you have further questions, I'm here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
last week
Abhay Jajodia answered

Hello Rishabh, really nice question.

When you use GROUP BY, you’re asking SQL to take many rows and squeeze them into one row per group. In your case, the groups are countries. That means all customers from the same country get combined so you can calculate something about the whole group — like the average age.

Here’s the key idea:
A grouped result needs one clear value per group.
The country has one value, the average age has one value
 but the name does not. There are many customer names in each country, so SQL doesn’t know which one to show.

That’s why this works:

SELECT country, AVG(age) AS average_age
FROM Customers
GROUP BY country;

But adding a name does not make sense unless you also group by it or aggregate it. If you tried:

SELECT country, name, AVG(age)

SQL would ask: “Which name should I show for this country? There are many.”

Once you see it that way, it becomes clear:
GROUP BY is for summaries, not individual details.

If you have further questions, I'm here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Kelish Rai
Expert
last week
Kelish Rai answered

Hello OTM Egytrans, really nice question.

In SQL, DISTINCT doesn’t belong to just one column once you write it — it applies to the entire set of selected columns together.

So:

SELECT DISTINCT col1
FROM your_table;

gives you unique values of col1.

But:

SELECT DISTINCT col1, col2
FROM your_table;

now treats each (col1, col2) pair as a single combination. SQL removes duplicate rows where both col1 and col2 are the same. It does not make col1 distinct while allowing col2 to vary freely in that same query.

That means there’s no direct way to say “make column 1 distinct, but don’t apply DISTINCT to column 2” in one simple SELECT DISTINCT col1, col2 statement.

If you want “distinct by column 1” and still show something from column 2, you usually:

  • either decide which row per col1 you want (for example, with MIN(col2) or MAX(col2) plus GROUP BY col1), or

  • use more advanced techniques (like window functions) depending on your database.

But the key idea is:
DISTINCT always works on the whole selected row, not just a single column unless you only select that one.

If you have further questions, I'm here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
yesterday
Abhay Jajodia answered

Hi Rishabh,

Good question — I see where the confusion is coming from.

Your query is counting how many distinct brands exist in the filtered data. Since you're filtering for ByteCore and ZapTech, there are clearly two distinct brands. So COUNT(DISTINCT brand) should return 2 — and it does.

But if you're getting 3 as the result, then the issue might be that you’re actually counting distinct product names, not brands.

If your intention is to count unique products under those brands, you should change the query to this:

SELECT COUNT(DISTINCT name) AS distinct_product  
FROM Products  
WHERE brand = 'ByteCore' OR brand = 'ZapTech';

This will count the number of different product names from those two brands — for example: keyboard, mouse, and headphone — which would give you 3.

So check what exactly you’re trying to count: brands or product names. That makes all the difference.

If you have more questions, I’m here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
yesterday
Abhay Jajodia answered

Hi Bruno,

Great question. Both NOT and <> / != can be used to exclude values, but there are cases where NOT is preferred for clarity.

Here’s when using NOT can make more sense:

  1. With complex conditions
    When you're excluding entire conditions or expressions, NOT can make things clearer:

    WHERE NOT (age < 18 OR country = 'UAE')
    
  2. With subqueries
    NOT IN and NOT EXISTS are common and often easier to read than alternatives:

    WHERE NOT EXISTS (SELECT * FROM ...)
    
  3. Improved readability
    In some cases, AND NOT country = 'UAE' might be easier to read than country != 'UAE', especially in long conditions.

That said, if you're just comparing single values, <> or != is shorter and works just as well:

WHERE country != 'UAE'

In the end, it comes down to readability and personal or team preference. Functionally, both work — pick the one that makes your intent clearest.

If you’ve got more questions, I’m here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
yesterday
Abhay Jajodia answered

Hi Tom,

Yes, exactly — WHERE always comes before GROUP BY in SQL.

Here’s why: the WHERE clause filters the rows before any grouping happens. So only the rows that match your condition are included in the groups.

Let’s say you have this query:

SELECT department, MIN(age) AS min_age, MAX(age) AS max_age
FROM Employees
WHERE department <> 'Marketing'
GROUP BY department;

First, SQL filters out all rows where the department is 'Marketing'. Then it groups the remaining rows by department and calculates the min and max age for each group.

If you applied the WHERE after GROUP BY, it wouldn’t work — because WHERE doesn’t operate on groups. That’s what HAVING is for, if you ever need to filter after grouping.

So yes, WHERE always comes first.

If you have more questions, I am here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Chika Lucinda Barn-Nzekwe
PRO
last month
Chikacountry asked
Kelish Rai
Expert
yesterday
Kelish Rai answered

Hi Chika,

int stands for integer, which means whole numbers — no decimals. Examples include 5, 42, or -100.

In most programming languages, including C and C++, int is used to declare variables that store whole numbers:

int age = 25;

That line creates a variable named age that holds the value 25.

If you have more questions, I am here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
yesterday
Abhay Jajodia answered

Hi Julia,

Yes, that’s correct — if you don’t use a TCL command like COMMIT, your changes may not be saved permanently.

When you run SQL statements like INSERT, UPDATE, or DELETE, the changes happen in a temporary state called a transaction. They're not actually saved to the database until you explicitly commit them:

COMMIT;

If you don’t run COMMIT, and you close the connection or something goes wrong, those changes can be rolled back — meaning they’re lost.

You can also use:

  • ROLLBACK to undo changes

  • SAVEPOINT to mark specific points you might roll back to

So yes — using TCL commands like COMMIT is what finalizes your changes.

If you have more questions, I am here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
yesterday
Abhay Jajodia answered

Hi there! This is a great question and it's quite common for SQL learners to wonder about these nuances.

When writing a GRANT or REVOKE clause, the use of * and ALL TABLES might seem similar because they both refer to permissions applied to all tables within a database. However, depending on the SQL database system you are using, they might function slightly differently and could have different levels of compatibility and specificity.

- ***:** This is sometimes used as shorthand to represent operations across all tables in a database, but it heavily depends on the SQL database implementation you're dealing with. It's often more associated with selecting all columns in a SELECT statement than granting permissions. - **ALL TABLES:** This statement is very clear in intent, explicitly specifying that the action should apply to all tables. It's generally more explicit and more reliably interpreted across different SQL systems for data control operations.

Given this, ALL TABLES is the more universally accepted syntax for granting/revoking permissions across all tables in most SQL systems, which provides less ambiguity in cross-database usage.

Please, let me know if you have any further questions or if you'd like to see more examples. Hope this helps!

SQL
This question was asked as part of the Learn SQL Basics course.