Question: trying executing this code on the group with joins but its giving me error that its not a group by statement

code below

```
     SELECT 
        sum (quantity),   
     customers.customer_name,
     states.state_name,       
     regions.region_name

      FROM sales  JOIN customers  ON sales.customer_id = customers.customer_id
join states on customers.state_id = states.state_id 
join regions on states.region_id = regions.region_idhaving sum(quantity) >= 10  

 GROUP BY   sales.sale_id, customers.customer_name, states.state_name, regions.region_id
 ;

```

saying not a group by expression.

Answer: 
```
dat <- data.frame(VAR1=c(0,1,0,1,0,0,1,1,0),
              VAR2=c(1,1,0,1,0,0,1,0,1), 
              VAR3=c(0,1,1,1,1,0,1,1,1))

dat1 <- dat[,names(sort(colSums(dat), decreasing = TRUE))]

dat1
   VAR3 VAR2 VAR1
1    0    1    0
2    1    1    1
3    1    0    0
4    1    1    1
5    1    0    0
6    0    0    0
7    1    1    1
8    1    0    1
9    1    1    0

```
```
dat[,names(sort(colSums(dat), decreasing = T))]

```

colSums gives sums of all columns of the *dat*

```
> colSums(dat)

VAR1 VAR2 VAR3 
 4    5    7 

```

sort it in a decreasing order of sum value

```
> sort(colSums(dat), decreasing = T)

VAR3 VAR2 VAR1 
7    5    4 

```

Get the names in this exact order and display *dat* columns in the same order 

```
> dat[,names(sort(colSums(dat), decreasing = T))]

    VAR3 VAR2 VAR1
1    0    1    0
2    1    1    1
3    1    0    0
4    1    1    1
5    1    0    0
6    0    0    0
7    1    1    1
8    1    0    1
9    1    1    0

```